Preg Replace acting very strange ???

Started on the old forum in December 2011 · view the original in the archive · old links redirect here

7 replies · 1,000 views · started 14 years ago · 1 min read

4 participants
something else Fellow · 2,296 posts #1

I Have A string that has got 2 links in it that are identical:

$str = '**** somewhere more text here .... **** somewhere';

im using Preg replace like this:

$str = preg_replace('/[/is','<a href="**?page**=-$1-**static-words**-$2.html">',$str);

this should echo the same on both links but it only changes the second link: output:

<a href="-randomtext-**static-words**-948191.html"> somewhere](#) more text here .... somewhere

However if i change the preg replace to this: $str = preg_replace('/[/is','<a href="**?page**=-$1-**testing-123**-$2.html">',$str);

the output changes to: <a href="-randomtext-**testing-123**-948191.html"> somewhere](#) more text here .... somewhere

what the hell is going on??? .... how can preg replace match the expression and yet only replace half of the string??

GumSlone Administrator Principal · 1,690 posts #2

something else wrote: I Have A string that has got 2 links in it that are identical:

$str = '**** somewhere more text here .... **** somewhere';

im using Preg replace like this:

$str = preg_replace('/[/is','<a rel="nofollow" href="**?page**=-$1-**static-words**-$2.html">',$str);

this should echo the same on both links but it only changes the second link: output:

<a href="-randomtext-**static-words**-948191.html"> somewhere](#) more text here .... somewhere

However if i change the preg replace to this: $str = preg_replace('/[/is','<a rel="nofollow" href="**?page**=-$1-**testing-123**-$2.html">',$str);

the output changes to: <a href="-randomtext-**testing-123**-948191.html"> somewhere](#) more text here .... somewhere

what the hell is going on??? .... how can preg replace match the expression and yet only replace half of the string??

a php bug?

with

$str = '<a href="-randomtext-static-words-948191.html"> somewhere </a>

 more text here ....
 
<a href="-randomtext-static-words-948191.html"> somewhere </a>';

$str = preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-static-words-$2.html">',$str);

i get this as output:

<a href="?page=-randomtext-static-words-948191.html"> somewhere </a> more text here .... <a href="?page=-randomtext-static-words-948191.html"> somewhere </a>

Added after 3 minutes:

Im not sure but try to add limit -1 to your preg replace like

$str = preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-static-words-$2.html">',$str,-1);

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

something else Fellow · 2,296 posts #3

i tried limiting it to 1 and it still didnt work....

Here`s the script ....

it does look like a bug in PHP to me unless im missing something..

arnage Lead · 811 posts #4

Try this bro with suppressed dot.

$str = preg_replace('/<a href="-(.*?)-static-words-(.*?)\.html">/is','<a href="?page=-$1-static-words-$2.html">',$str);
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
something else Fellow · 2,296 posts #5

it matched it again but still only replaces half the url ..... it really doesnt make sence

just_m3. Developer · 199 posts #6

yeah strange **** 🙂 i also tried to fix it, it din't replaced all manny ways none worked ..

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

something else Fellow · 2,296 posts #7

arnage wrote: Try this bro with suppressed dot.

$str = preg_replace('/<a href="-(.*?)-static-words-(.*?)\.html">/is','<a href="?page=-$1-static-words-$2.html">',$str);

I think the answer is suppressed minus 😀 but havent tried it 🙂

arnage Lead · 811 posts #8

Quantifiers are: \ | ( ) [ { ^ $ * + ? . < >

Try this bro and if not there can only be problem in quotes with $1 or $2.

$str = preg_replace('/\<a href="-(.*?)-static-words-(.*?)\.html"\>/is','\<a href="\?page=-$1-static-words-$2\.html"\>',$str);
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close