Jump to content

PCRE code


barakk

Recommended Posts

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

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

<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

<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

<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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.