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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.