Wednesday, 15 June 2011

Replace a particular content in a string using regex php -



Replace a particular content in a string using regex php -

i have string like

some text test , need replace regex using php have sentence too

i need replace sentence before word regex

i need result of

using php have sentence too

replace some text test , need replace regex ""

php can utilize \k

use simple regex:

(?s)^.*?regex\k.*

see demo.

the (?s) allows dot match across several lines. ^ asserts @ origin of string. .*?regex lazily matches regex \k tells engine "keep" has matched far out of returned match (drop it) .* matches else. returned match.

in php, no need replace. output of php demo shows, can match:

$mystring = "some text test , need replace regex using php. have sentence"; $regex = '~(?s)^.*?regex\k.*~'; if (preg_match($regex, $mystring, $m)) { $yourmatch = $m[0]; echo $yourmatch; }

let me know if have questions. :)

php regex string

No comments:

Post a Comment