Msg#: 3814557 posted 6:45 pm on Dec 26, 2008 (gmt 0)
I only want to replace the word with the link.
You have to grab the rest of your pattern then and store it as well so you can use it in the replacement. Note the additional parentheses added to the pattern and the use of the variables in the replacement:
$patterns = "/(<p>.*\b)(hello)(\b.*<\/p>)/is"; $replacements = "$1AAA$3"; // for testing
I used word boundaries here rather than space characters as the word may appear at the beginning or end of the element text with no space characters between it and the end. For example ... <p>hello</p>
Msg#: 3814557 posted 8:53 pm on Dec 26, 2008 (gmt 0)
Yeah, I was thinking about that when I threw the expression down the first time. I was actually going to do that for you but when you said the word was in paragraph tags I figured that was it, nothing more, so I left it as is. Are you certain you want your second tag marker like that? The regex as is with the end tag marker match of [^>]* will not match on something like
<p>Hello there <b>darkage</b>!</p>
I would think you will want to keep the dot metacharacter there. If not, since you are no longer using the dot metacharacter you can drop that "s" modifier.
You shouldn't need that ungreedy modifier. Were you having unexpected results without it?
Msg#: 3814557 posted 9:35 pm on Dec 26, 2008 (gmt 0)
The ungreedy modified is due to having several paragraphs <p></p> so I want to make the "smallest" match, which might not be needed since I negating the '<' and '>'.
I have an issue though with negating the '<' and '>'. I cant replace multiple keywords with links within the same paragraph as once the first replacement is done, it now contains <a href="" ... so all other matches on that paragraph fails.
Have to look into what's the best way to solve it (avoid replacing keywords that have already been replaced and thus are a link or in other words avoid links in links - get it? :-).
Msg#: 3814557 posted 3:26 pm on Dec 27, 2008 (gmt 0)
Depending on which version of PHP you are running, you may be able to use Recursive patterns [php.net]. They are a bit more advanced but pretty powerful once you get a grip on them. The PHP manual page I linked to there has some information but the perlre [perldoc.perl.org] manual page has much more information.