Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression question 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR
Can one use str_replace to do this:

Code:
$string=' AND VALUE=..... AND .....';

should end up removing the leading AND

In perl this would be
Code:
$string=~s/^\s+AND//
or
Code:
$string=~s/^AND//
(yes, it can be do in one statement) to remove a leading 'AND' and possibly any whitespaces(blancs) before it.
 
I'm confused, ....

are you asking a question?

or giving us an answer?

(yes, it can be do in one statement)

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
It is a question:
Question is : How do I remove it in PHP?
'Answer' is how I would do it in PERL. So I know how to do it in perl and an looking for a php solution
 
Hi

Use [tt]preg_replace()[/tt] :
PHP:
[COLOR=#6666FF]$string[/color] [COLOR=#008080]=[/color] [COLOR=#FF6600]preg_replace[/color][COLOR=#008080]([/color][i][COLOR=#009900]'/^\s+AND/'[/color][/i][COLOR=#008080],[/color] [i][COLOR=#009900]''[/color][/i][COLOR=#008080],[/color] [COLOR=#6666FF]$string[/color][COLOR=#008080]);[/color]


Feherke.
feherke.ga
 
Thanks. If $string does not start with \s+AND, I assume this will not change anything, correct?
 
Hi

Correct. PHP's [tt]preg_*()[/tt] functions use the PCRE ( Perl-Compatible Regular Expression ) library, so you can expect the behavior you are used with from Perl.


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top