Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match Help

Status
Not open for further replies.

oohoohoohooh

Programmer
Apr 30, 2005
55
0
0
GB
Hi, I was wondering how you strip the contents of a string uptil the first occurance of <a and also how to strip a string to the last occurance of </a>

ie

$string = '<body>
<a href="test.htm">test</a>
<a href="text2.htm">test2</a>
</body>';

will return:

$string = '<a href="test.htm">test</a>
<a href="text2.htm">test2</a>';

Appreciate it if someone could help. Thanks
 
Hi, I need to include the content in between the <a> tags that's why regular expressions is the way to go. Cheers anywayz
 
did you *try* using strip_tags? this does *exactly* what you're asking:
Code:
$string = '<body><a href="test.htm">test</a><a href="text2.htm">test2</a></body>';

//  $foo will contain the string:
//  '<a href="test.htm">test</a><a href="text2.htm">test2</a>'
$foo = strip_tags($string, "<a>");

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Hi, when I ran it I got testtest2 but I want:

$string = '<body>
<a href="test.htm">test</a>
<a href="text2.htm">test2</a>
</body>';

to return:

$string = '<a href="test.htm">test</a>
<a href="text2.htm">test2</a>';
 
Odd, the example worked for me. I copied and pasted it into a file, added some script tags, worked perfect...?

barcode_1.gif
 
if you echo the string '<a href="test.htm">test</a>
<a href="text2.htm">test2</a>' to a browser, of course it will show up as testtest2 because the browser is interpreting the HTML!

this doesn't change the fact that the string actually contains the text: <a href="test.htm">test</a>
<a href="text2.htm">test2</a>

try echoing inside a <textarea> to see...

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
It still strips all the html out in between though so it isn't gonna work. Any other idea?
 
what html?

are you just trying to harvest links from a chunk of html?



-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
here's two ways:
Code:
$str = <<<END
<p>foo</p>
	<a href="page.html">page</a>
<ul>
	<li>1</li>
	<li>2</li>
</ul>
<div>div</div>
<span><a href="two.html">two</a></span>
END;

	//  harvest only links:
	$matches = null;
	preg_match_all('/<a href="[^"]*">[^<]*<\/a>/', $str, $matches);
	
	
	//  extract from first <a to last </a>:
	$start = stripos($str, "<a");
	$end = strripos($str, "</a>") + 4;
	$len = $end - $start;
	$new_str = substr($str, $start, $len);

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top