barakk Posted June 27, 2008 Share Posted June 27, 2008 Hi, I have some problems with getting the address from a <A> tag. I made this little function: function href_d($string) { preg_replace("#<a[?: href=('|\")((\w:/\.\?\#&=\-)*)\\2]?[\w\"'_\s]*>#iU","1 \\2",$string); htmlspecialchars($string); return $string; } but... There is some error. Its retrieving me the same string i entered. Any one see the problem? Ty Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/ Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 function href_d($string) { $string = preg_replace("#<a[?: href=('|\")((\w:/\.\?\#&=\-)*)\\2]?[\w\"'_\s]*>#iU","1 \\2", $string); $string = htmlspecialchars($string); return $string; } Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-575926 Share on other sites More sharing options...
barakk Posted June 27, 2008 Author Share Posted June 27, 2008 function href_d($string) { $string = preg_replace("#<a[?: href=('|\")((\w:/\.\?\#&=\-)*)\\2]?[\w\"'_\s]*>#iU","1 \\2", $string); $string = htmlspecialchars($string); return $string; } First..TY. but its still doesn't work. I entered this string: $str = "testrsefr sdf sdfsdfsdfsd <a href= 'http://www.walla.co.il'>sdf sdfds fsdf </a> esdfgsdg<a href='http://google.co.il''>dxfgdfgf</a>"; and its showing me the same string. Ty Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-575946 Share on other sites More sharing options...
effigy Posted June 27, 2008 Share Posted June 27, 2008 <pre> <?php function href_d($string) { preg_match_all('/<a[^>]+href\s*=\s*([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/i', $string, $matches); return $matches[2]; } print_r(href_d("testrsefr sdf sdfsdfsdfsd <a href= 'http://www.walla.co.il'>sdf sdfds fsdf </a> esdfgsdg<a href='http://google.co.il''>dxfgdfgf</a>")); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-575974 Share on other sites More sharing options...
barakk Posted June 27, 2008 Author Share Posted June 27, 2008 <pre> <?php function href_d($string) { preg_match_all('/<a[^>]+href\s*=\s*([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/i', $string, $matches); return $matches[2]; } print_r(href_d("testrsefr sdf sdfsdfsdfsd <a href= 'http://www.walla.co.il'>sdf sdfds fsdf </a> esdfgsdg<a href='http://google.co.il''>dxfgdfgf</a>")); ?> </pre> Thank you! Great way to do it! how can I find the wird after the <a>? Just $matches[3] in the function? Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-575999 Share on other sites More sharing options...
effigy Posted June 27, 2008 Share Posted June 27, 2008 <pre> <?php function href_d($string) { preg_match_all('/ <a[^>]+ href\s*=\s* ([\'"])?((?(1).+?|[^\s>]+)) (?(1)\1)[^>]*> (.*?)(?=<\/a>) /xi', $string, $matches); return array_splice($matches, 2, 2); } print_r(href_d("testrsefr sdf sdfsdfsdfsd <a href= 'http://www.walla.co.il'>sdf sdfds fsdf </a> esdfgsdg<a href='http://google.co.il''>dxfgdfgf</a>")); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-576013 Share on other sites More sharing options...
barakk Posted June 27, 2008 Author Share Posted June 27, 2008 Thank you man! You are great! Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-576218 Share on other sites More sharing options...
barakk Posted June 27, 2008 Author Share Posted June 27, 2008 just 1 question... I didnt understand how the last function works?... If I want to print only the first URL and the text what should i do? I never worked with double array in php. Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-576230 Share on other sites More sharing options...
effigy Posted June 27, 2008 Share Posted June 27, 2008 Review them; they're very important. $data = href_d("testrsefr sdf sdfsdfsdfsd <a href= 'http://www.walla.co.il'>sdf sdfds fsdf </a> esdfgsdg<a href='http://google.co.il''>dxfgdfgf</a>"); echo $data[0][0]; Link to comment https://forums.phpfreaks.com/topic/112195-pcre-code/#findComment-576236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.