Jump to content

regex for stopping at either tags


chick3n

Recommended Posts

String 1: "<tag>text in here<ins>more text</tag>"

String 2: "<tag>text in here</tag>"

 

I want to write a expression that will stop at the end of the tag and get the text between it or stop at a predefined tag and get the text between that.

 

So in string 1 i want to get "text in here" and stop at <ins> and in string 2 i want the same text but stop at </tag> instead.

 

My regex for getting the text inbetween the tags is: /<span class=\"\">(.*)<\/span>/smU

But i cannot get it to do an either or type of thing with <ins> || </span>

 

Link to comment
https://forums.phpfreaks.com/topic/62852-regex-for-stopping-at-either-tags/
Share on other sites

<pre>
<?php

$tests = array(
	'<tag>text in here<ins>more text</tag>',
	'<tag>text in here</tag>',
	'<tag></tag>',
	'<tag>a</tag><tag>b</tag>'
);

foreach ($tests as $test) {
	echo htmlspecialchars($test), '<br>';
	preg_match('#<[^>]+>(.*?)<[^>]+>#', $test, $matches);
	echo htmlspecialchars($matches[1]);
	echo '<hr>';
}

?>
</pre>

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.