Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to do intelligent replacements

Status
Not open for further replies.

dontpanic

Technical User
Joined
Jan 27, 2004
Messages
2
Location
US
I'm trying to use the preg_replace command to do replacements of specific strings with the string as a URL.

An example from what I am using:

Code:
$post['message'] = preg_replace('/(70-292)/i', '<a href=&quot;[URL unfurl="true"]http://www.mcseworld.com/a/1932266569/&quot;[/URL] target=&quot;_blank&quot;>\\1</a>', $post['message']);

This code would thus case the 70-292 string to be replaced with that URL.

I have two issues:

1. I cannot quite figure out how to get ONLY exact matches, no matter where they might appear in the post. I've been reading on this here: and here:
2. I think that I will need a if/then loop to prevent the substitution from being made if the string is already encased in <a> or tags, but I am not sure how to go ...information you may need. Thanks in advance!
 
Let's try to clarify what you want to replace:

All occurences of 70-292 unless the string is inside an <a href....> tag.

Right?
 
Yes, that is correct. I want replace all occurance except that that are already encased in <a> or tags. As an example, it behaves i...rtification to Microsoft Windows Server 2003 [/code]

What I've been able to get thus far would break this URL at the point of &quot;70-292&quot; and perform the replacement on that string. So we'd wind up with one URL for the text up to the string of &quot;70-292&quot; and a second URL for the replacement string. The text after the point of the string would thereafter not be part of a URL.

The desired effect would be to completely ignore this instance of the string.

As another example, say a user posted this:
Code:
Be sure to check out the <a href=&quot;[URL unfurl="true"]http://www.microsoft.com/traincert/exams/70-292.asp&quot;[/URL] target=&quot;_blank&quot;>Preparation Guide for Exam 70-292</a>

We would get a slightly different effect. Now this URL would be completely broken and the string would be replaced. I want to completely ignore this instance of the string.

Does this help to clarify what I am after?

Thanks,
Will
 
Keep it simple and make 3 steps:

1. Replace all occurences within the offending tags with a munged string
2. Replace all remaining ocurrences
3. Replace all munged items with original

That will be faster than one large expression.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top