Forum Moderators: coopster

Message Too Old, No Replies

Preg replace and problem with patterns

         

Nicholas

12:09 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I am trying to get the following code to work, but there seems to be a problem with the "pattern" part of the code:


<?php
$params['site_url'] = 'http://www.mysite.com';

$string = '<a href="http://www.mysite.com/&amp;usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\"{$params["site_url"]})/(.*?)\"¦i';
$replacements = 'test';
$string = preg_replace($patterns,$replacements,$string);
echo $string;
?>


I think the problem is that I am using a variable inside the pattern?
Result of the above code is a empty $string. Can anyone help me fix the problem?

henry0

2:58 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nicholas, welcome to WebmasterWorld!

adding double quotes within { }
renders
[mysite.com...]

$patterns = '¦(href.*?=.*?\"{"$params["site_url"]"})/(.*?)\"¦i';

Beware WebmasterWorld break pipe as ¦

Nicholas

5:41 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Hi henry0,

Thank you for your reply. I am happy that I registered here.

Henry, adding double quotes didn't fix my problem. I assume that my code should output: <a test>test</a>, where as adding " within {} will output: <a href="http://www.mysite.com/&amp;usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>.
It looks like that PHP does not handle variables inside the pattern. I appreciate more help. Thanks.

henry0

6:17 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange, on my local test bed it does the job
on my live server test bed it works too
I see a blue activated link with the usual underline, that reads:
test

try to copy and paste the following code
with NON BROKEN PIPES

<?php
$params['site_url'] = 'http://www.mysite.com';

$string = '<a href="http://www.mysite.com/&amp;usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\"{"$params["site_url"]"})/(.*?)\"¦i';
$replacements = 'test';
$string = preg_replace($patterns,$replacements,$string);
echo $string;
?>

Nicholas

6:42 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Henry,
That is the problem. It shouldn't be a link. it should only display a test without any link.

henry0

6:53 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Reverse replacements and string
it gives you test without activated link
if this is not what you need, sorry but I don't get it
<?php
$params['site_url'] = 'http://www.mysite.com';

$replacements= '<a href="http://www.mysite.com/&amp;usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\{$params["site_url]})/(.*?)\"¦i';
$aa = 'test';
$string = preg_replace($patterns,$replacements,$aa);
echo $string;
?>

PHP_Chimp

7:04 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just look above.

[edited by: PHP_Chimp at 7:04 pm (utc) on July 17, 2008]

henry0

8:48 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HMMM, wondering what you had :)

Nicholas

11:57 pm on Jul 18, 2008 (gmt 0)

10+ Year Member



Henry,

Thank you for your reply.
It is my fault that I came up with a strange code here to resolve a bigger problem I had with another script :-( I think I should come up with a better example.
I had this strange behaviour that preg_replace could not find the pattern and replace it with "replacement" in another script which I use to clean up some URLs. I came here to get help, and had two choices:
1- Paste the original code with many functions.
2- write a new code, shorter, which reflected the same problem with the same pattern and ask for help.
And I chose the second option. I wrote a new script (above) which should change the URLs from a clean one to a strange thing! for example from <a href="http://www.example.com">Example</a> to something broken, which does not give any meaning: <a test">test</a> (doesn't mean anything!). And that caused you trouble of understanding what I really wanted.
So here I am, fixed my original problem. My server couldn't find the pattern because of a php value: pcre.backtrack_limit which is originally set to 100.000 (100kb). Moreover, I found out that many have already complained about this low value and there is also a bug report on php website. So I changed this value to 300.000 and resolved the problem.
Thank you and Thanks anyone who replied to this thread.