Forum Moderators: coopster
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/&usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\"{$params["site_url"]})/(.*?)\"¦i';
$replacements = 'test';
$string = preg_replace($patterns,$replacements,$string);
echo $string;
?>
adding double quotes within { }
renders
[mysite.com...]
$patterns = '¦(href.*?=.*?\"{"$params["site_url"]"})/(.*?)\"¦i';
Beware WebmasterWorld break pipe as ¦
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/&usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>.
It looks like that PHP does not handle variables inside the pattern. I appreciate more help. Thanks.
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/&usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\"{"$params["site_url"]"})/(.*?)\"¦i';
$replacements = 'test';
$string = preg_replace($patterns,$replacements,$string);
echo $string;
?>
<?php
$params['site_url'] = 'http://www.mysite.com';$replacements= '<a href="http://www.mysite.com/&usg=ALkJrhjH9kTNqMTdeNiO2A6v5J5dRmmsZA">test</a>';
$patterns = '¦(href.*?=.*?\{$params["site_url]})/(.*?)\"¦i';
$aa = 'test';
$string = preg_replace($patterns,$replacements,$aa);
echo $string;
?>
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.