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
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>

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.