Re: Getting part of string matched with regular expressions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Teck wrote:
Hi,


I'm trying to find a way to get part of a string where the part is matched with regular expressions.

So, for example, if I have a string:

a2b3cd5ef6ghi7232jklmn

I need to grab "12b3cd5" using regular expressions and store the part in a variable.

$var = do_something("","","a2b3cd5ef6ghi7232jklmn");

I was using preg_replace for this, and try to delete (i.e., replace the non-matched part with an empty string) the second part, but I can't make it work.

You can use backreferences in regular expressions.
$var= preg_replace('/([a-z]+)/is','$1','azxcvjf937^88');
if($var==='azxcvjf') echo "MATCH";

http://php.net/manual/en/function.preg-replace.php
look at Example1.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux