Forum Moderators: coopster

Message Too Old, No Replies

regular expression to match characters inside links

and replace them them

         

adam02

2:07 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



hello, I have little knowledge of regex, but I want to replace '&' and '=' from inside links.

Example:

$text = '<a href="http://www.site.com/index.php?param=123&param2=345"> stuff';

Anybody can help? I would really greatly appreciate it

adam02

5:53 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



[webmasterworld.com...]

found this thread which is probably the opposite regex to what I need.

Will try to understand how it works..

Salsa

6:29 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Welcome to Webmaster World, Adam.

Yep, that thread looks like it has a very addaptable example for your purpose. One thing to be aware of is that ? is a special character in regexs, so to use it literally, you'll need to escape it with a backslash: \?

<edit> Ooops, I was thinking you were replacing '?' when I wrote that, but it was '&' -- which you don't have to worry about.

adam02

2:58 pm on Dec 18, 2004 (gmt 0)

10+ Year Member



Hello

After studying this [devplanner.com...]

I came up w/ the following, but it is not matching a single thing ... :(

$text = '<a href="http://www.site.com/index.php?param=123&param2=345"> stuff';
$lookfor = "=";

$pattern = '#(<.*?)(\\B'.$lookfor.'\\B)([<>]*?>)#';
$replacement = '_eq_';
$text = preg_replace($pattern, $replacement, $text );

echo $text;

Salsa

4:13 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Adam, this should work for you. I think it should be possible to do this without
the loops because preg_replace will take arrays as arguments, but I have not been able to replace more than one instance of a pattern per tag at a time that way. Then, making use of the while loop makes it more difficult to make use of arrays as arguments, therefore the for loop. As it is, one instance will be replaced in each tag per loop. To your $text test string, I added some other stuff to test that the regex would not make replacements where not wanted, and included new lines to test for that case, too. Maybe someone will offer a more efficient way?

<?php 
$text = "<a
href=
\"http://www.site.com/index.php?param=123&param2=345&param3=678\">
<span style=\"color:red;\"> this=stuff, </span></a>
& this=more stuff, <a href=\"http://www.site.com/index.php?param=123&param2=345\">
&this=final stuff</a>";
echo htmlspecialchars($text)."<br>&nbsp;<br>\n\n"; //just to see with browser

$look_fors = array('=', '&'); 
$replace_withs = array('_eq_', '_AND_');
for($i = 0; $i < count($look_fors); $i++) {
$pattern = "#(<a[^>]*?)$look_fors[$i](.*?>)#s";
$replacement = "\${1}$replace_withs[$i]\$2";
while (preg_match($pattern,$text)) {
$text = preg_replace($pattern, $replacement, $text );
}
}
echo "$text<br>&nbsp;<br>\n\n";
echo htmlspecialchars($text)."<br>&nbsp;<br>\n";
?>

The '<a' in $pattern will cause the replacements to be made only in anchor tags.
If you want them to be made in all tags, simply remove the 'a'. If you don't
what to replace the '=' in '<a href=', simply include all of that in $pattern,
etc.

I hope this helps.

adam02

4:04 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



thanks Salsa! it looks great ;)
I've been struggling all weekend w/ regular expressions in loops ..

I will test it as soon as I can find time, during the week.