I have this php code that's supposed to extract the url if it finds a match in the anchor text: function get_url ( $string ) { if ( preg_match ( '/<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*:))([^"\']+)[^>]*>(audi)<\/a>/si', $string, $link ) ) { return $link [ 1 ]; } else { return FALSE; } } PHP: well....it works on the following example: get_url('<a href="asdasd">audi</a>') PHP: but not on this one: get_url('<a href="asdasd">audi something here</a>') PHP: anyone good at regex?
Yes, because you have audi hardcoded in the anchor text why don't you use if ( preg_match ( '/<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)([^"\']+)[^>]*>(audi)<\/a>/si', $string, $link ) ) if ( preg_match ( '/<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)([^"\']+)[^>]*>(.*?)<\/a>/si', $string, $link ) ) It should work.
I have this php code that's supposed to extract the url if it finds a match in the anchor text: that match is audi...so I need a function that will extraxt all the url's that have audi somewhere in the anchor.
Oh I see try this if ( preg_match ( '/<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)([^"\']+)[^>]*>(.*?)audi(.*?)<\/a>/si', $string, $link ) )
MakeADifference's solution incorrectly matches "audio", "auditor", "Saudi Arabia" and probably many others. I would use: /<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)[^"\']+[^>]*>(audi|.+?\Waudi\W.+?)<\/a>/si
Sorry, use this instead: /<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)[^"\']+[^>]*>(audi|.*?\Waudi|.*?\Waudi\W.*?|audi\W.*?)<\/a>/si Or a slightly simplified version: /<a[^>]+href\s*=\s*["\'](?!(?:#|javascript\s*)[^"\']+[^>]*>(.*?\Waudi|audi)(\W.*?<\/a>|<\/a>)/si I tested to make sure this time. If, heaven forbid, it doesn't work, then can you please tell me what string you tried to match it against?
Thank you krt. Works now but with a small problem. It doesn't gets the link out...but the whole code instead of : http://www.google.com it gets: <a href="http://www.google.com">audi</a> Hope I'm being clear with what I need and not confusing you. Thank you.
function get_url($string) { if (preg_match('/<a[^>]+href\s*=\s*(["\'])((?!(?:#|javascript\s*:))[^"\']+)\\1[^>]*>(.*?\Waudi|audi)(\W.*?<\/a>|<\/a>)/si', $string, $link)) { return $link[2]; } else { return false; } } PHP: