Forum Moderators: coopster
I have a regex that seems to do a match for the href portion, and enables me to replace the entire link with ordinary text with preg_replace. But this won`t match the link-text portion.
$regex = "#(<a[^>]*?)$value(.*?)<\/a>#si";
And this regex seems to do a match for link-text, but when I try to preg_replace the entire link, it only replaces the link-text portion and not the entire anchor tag.
$regex = "#(?!<a*?)($value)(.*?</a>)#si";
basically I`d like to find string matches with everything contained within the open and close anchor tags <a ,/a>, and replace the entire anchor tag if any matches are found. which I think would probably suffice my needs. unless someone can easily comeup with some href/link-text matching regex.
Any help would be appreciated.
[edited by: eelixduppy at 8:56 am (utc) on Nov. 27, 2008]
[edit reason] disabled smileys [/edit]
<a class="mylink" href="http://www.example.com/page.htm">My Page</a>
for example say I am looking for a match(case insensitive) for the string "my", "page" or ".com" I would like to replace the anchor code below with just the text "test"(no tags, nothing, just text).
<a class="mylink" href="http://www.example.com/page.htm">My Page</a>
I`m assuming that with the strings I am looking for(e.g. ?a=r), it is unlikely that a match would occur in the attributes but I`m just compromising with what I think is the easiest solution as I am so stuck with this.
If a string comparison can be made in just the href and link-text that would be perfect (I just couldn`t get anything to work like that). If not something that can find matches in the entire anchor tag would still work for me.
This was what I was trying to get to work, if anyone can help..
[webmasterworld.com...]
$regex = "/<a\s[^>]*href\s*=\s*([\"\']?)(".$filter_string."[^\" >]*?)\\1[^>]*>(.*)<\/a>/siU";
[edited by: SarK0Y at 3:43 pm (utc) on Dec. 6, 2008]
Say if I was trying to find/replace certain links in a webpage how would I go about it with preg_match_all, as preg_match_all only stores the matches right? (currently I`m going with preg_replace)
Also if I understand your suggestion correctly, I think your solution will find all the anchor tags in a web page. I`m only looking for certain links with specific strings in them, so that`s why I have a php variable in the regex.
Let me know if I understood you correctly.
[edited by: SarK0Y at 12:35 am (utc) on Dec. 7, 2008]
I've used this (or a version of it - sometimes I need to mess with it a little bit) several times and it works nicely:
preg_replace('/<p>\s*<!--(.*)-->\s*<\/p>/i')
the section with "<--(.*)-->" basically looks for a string in my content that looks similar to a comment, such as <-- text here -->. The (.*) part covers the "text here". it can be anything. it's just looking for the "<--" at the beginning and "-->" at the end, and replaces anything in between it with whatever I want.
It also looks for <p> at the beginning and end...the \s* handles anything after the "<p"> and before the "<--"
Hopefully that's what you're looking for :)
Doodlebee, do you know any ways to replace the entire "<!--(.*)-->" section with whatever you want, rather than what is between the "<--" "-->" comment tags?
I might be able to work with that, if I can put a php variable in it. Which is my other question. If I was using DOS style wildcards (*), I`d be looking for the regex equivalent of *$match_string*, as in find any string that has $match_string in it regardless of what is before or after the $match_string ( and in between the <a> tags). If match is found then replace the anchor tag and what is in between with something.
Currently I`ve come up with versions of *?$value(.*?) like in the first post in this thread, but I can only match the link and not match with link-text, or not replace the entire anchor tag.
Basically I want to replace a link with a string if a certain kind of string is found between the anchor tags.
[edited by: Scooter at 10:11 am (utc) on Dec. 7, 2008]
preg_replace('/<a\s*(.*)\s*<\/a>/i','replacement text here',$content); I *think* that might do it (the "\s" is basically for spaces, and the * is a wildcard) - but you'll have to play with it and see.
Just for the record, the base thing ("
preg_replace('/<p>\s*<!--(.*)-->\s*<\/p>/i', 'replacement', $content)") is what I use is a function for WordPress stuff. So when I'm in a WordPress post, I can do something like "<-- command here -->" and the function searches the post ($content) for the stuff above, and replaces it with 'replacement'. (Hopefully that gives you a better idea of what I use it for.) It's very versatile. I might be able to work with that, if I can put a php variable in it.
you should be able to. Going on what I said before:
preg_replace('/<a\s*(.*)\s*<\/a>/i','$var',$content); for if you want what it's replaced with to be a variable. I can't imagine the link itself would be a variable though!
I`d be looking for the regex equivalent of *$match_string*, as in find any string that has $match_string in it regardless of what is before or after the $match_string...
You might be looking for strpos(); so you can do:
if(strpos($content,$var) != '') {
preg_replace('/<a\s*(.*)\s*<\/a>/i','$var',$content);
}
where $var would be what you're looking for, and if it's found, then do the replacement. strpos() (if I remember correctly) will looks for the exact characters (case-sensitive...stripos() for insensitive), and if they aren't alphanumeric, it'll turn it into either "1" or "0". it's a little tricky sometimes - I find a lot of times I have to use the "===" instead of "!=" or "==" to make it work.
Basically I want to replace a link with a string if a certain kind of string is found between the anchor tags.
If I were to write a function for this, it'd probably look something like this:
function find_link($content) {
$find = '<a' . (*) . '</a>';
$replace = 'text replacement here';
if(stripos($content,$find) != '') {
$content = preg_replace($find,$replace,$content);
}
return $content;
}
if it's a particular *kind* of link - for example, a certain site you're looking for, then you could change the above $find to:
$find = $find = '<a' . (*) 'somesite.com/' . (*). '</a>'; I don't know how accurate that is - it's totally untested (and I'm currently experiencing PHP brainfry this weekend - so I'd expect if you tried this right out of the box, it'll put out errors, knowing my luck this week) but it should give you a starting point. I'm also not quote sure how you'd actually make the function run - I typically code for WordPress, and that has built-in functions that you can readily use for custom coding (in the above case, I'd end it with "
add_action('find_link',$content)" so it would search posts for the link). But that should give you something to go on anyway.
wondering if I need something like this:
regex = "#(<a[^\>]*?$value.*?\>)$value(.*?)<\/a>#si";
(but this doesn`t match anything.)
[edited by: eelixduppy at 6:26 pm (utc) on Dec. 9, 2008]
[edit reason] disabled smileys [/edit]
Array
(
[0] => <a id='5' href='my.com' >
[1] => 5
[2] => my.com
)
[edited by: SarK0Y at 12:17 am (utc) on Dec. 10, 2008]
[edited by: eelixduppy at 12:32 am (utc) on Dec. 10, 2008]
[edit reason] formatting [/edit]