Johnk
Johnk

Reputation: 13

Multiple regex statements

I am using multiple regex statements with preg_replace and found out that they work individually. Once I include more than one preg_replace, another will stop working.

Is there a way to use preg_replace in an ordered way, rather than as soon as it loads?

Here's what I have so far:

file_put_contents("$uploadedfile", preg_replace("/<[^>]*>/", "", "$filecontents"));         

file_put_contents("$uploadedfile", preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '<em>(\1) \2-\3</em>', $filecontents));

Upvotes: 1

Views: 261

Answers (1)

Venu
Venu

Reputation: 7279

Because filecontents which you are using in second is old one. you won't see the effect of first statement as you were not fetching the new content from file.

try this

file_put_contents("$uploadedfile", preg_replace("/<[^>]*>/", "", "$filecontents"));  

$filecontents = file_get_contents($uploadedfile);       

file_put_contents("$uploadedfile", preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '<em>(\1) \2-\3</em>', $filecontents));

or

save everything to one temp variable and write to file at the end

Upvotes: 1

Related Questions