如何通过指定开始和结束字符从字符串中子字符串? [关闭]

I have some texts that are always begin with an image tag , so i want to print the text without the image by specifying the start and the end characters of the string that should be removed and get the rest of the text, something like:

explode($text, '<img', '/>'); // where explode($string, $start_chars, $end_chars);

for example:

$text = "<img src='anything' width='100' height='200'/><h1>Hello World!</h1>";

the output should be <h1>Hello World!</h1>

so how I can do that in php?

seems to be the optimal answer:

$dom = new DOMDocument();
$dom->loadHTML($m->en);
$node = $dom->getElementsByTagName('img')->item(0);
$node->parentNode->removeChild($node);
$string = $dom->saveHTML();
echo $string;

You may have not heard of the substr() function of PHP. It is indicated in here!

http://php.net/substr

Revised based on new question...

Use DOMDocument:

$text = "<img src='anything' width='100' height='200'/><h1>Hello World!</h1>";
$dom = new DOMDocument();
$dom->loadHTML($text);
$h1Tags = $dom->getElementsByTagName('h1');
$string = $dom->saveHTML($h1Tags->item(0));
echo $string;

Output: <h1>Hello World!</h1>

See here for more info / examples

Given some text like:

<img src='img1.png' width='100' height='200'/><h1>Title 1</h1>
<img src='img2.png' width='100' height='200'/><h1>Title 2</h1>
<img src='img4.png' width='100' height='200'/><h1>Title 3</h1>

You state you want to collect just the text that would appear between the IMG tags. This was not clear upfront, and was suggested, you can use the DOMDocument to parse the HTML.

Using Regular Expressions is another way to go. Example: https://regex101.com/r/kH0xA3/1

$re = "/<*img.*\\/>(.*)/im"; 
$str = "<img src='img1.png' width='100' height='200'/><h1>Title 1</h1>
<img src='img2.png' width='100' height='200'/><h1>Title 2</h1>
<img src='img4.png' width='100' height='200'/><h1>Title 3</h1>"; 

preg_match_all($re, $str, $matches);

Try with this:

$text = "<img src='anything' width='100' height='200'/><h1>Hello World!</h1>";

$dom = new DOMDocument();
$dom->loadHTML($text);

$node = $dom->getElementsByTagName('img')->item(0);
$node->parentNode->removeChild($node);

$dom->removeChild($dom->doctype);           
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);

echo $dom->saveHtml();