-3

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?

Mohammad
  • 3,449
  • 6
  • 48
  • 75
  • 1
    please show example text and desired output –  May 21 '15 at 22:59
  • 1
    Can do it the hard way with string functions. I would advise regular expression. Discussed here: http://stackoverflow.com/questions/2180255/matching-src-attribute-of-img-tag-using-preg-match – Twisty May 21 '15 at 23:00
  • @Twisty I hope that there is a better solution, thanks – Mohammad May 21 '15 at 23:05
  • 2
    all you need for your example is: `strip_tags($text)` –  May 21 '15 at 23:05
  • @Dagon strip_tags() will remove all the tags, I want to remove only the first img tag – Mohammad May 21 '15 at 23:07
  • there only is one. it does exactly what you ask based on your example –  May 21 '15 at 23:08
  • @Dagon the text after the img contain a lot of html tags, I just wrote a small example – Mohammad May 21 '15 at 23:10
  • don't make it hard to answer you, your asking for free help. I'm stopping my paid work to assist you - but i'm going back to it now. –  May 21 '15 at 23:11
  • @Dagon lol, I appreciate that, thank you, but I didn't the suitable answer tell yet – Mohammad May 21 '15 at 23:13
  • Your questions was not clear. It come off as wanting the text in the source. Not just popping off the IMG tag from a much larger string of HTML. Please give a better example of what is being worked with. – Twisty May 21 '15 at 23:21
  • simply, I have a large html text started with an img tag, i want to remove the img tag from the text only ! – Mohammad May 21 '15 at 23:24
  • but in your example `

    ` must be removed too?

    – Federkun May 21 '15 at 23:25
  • @Leggendario, no just the first img tag should be removed, thanks – Mohammad May 21 '15 at 23:30

5 Answers5

1

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

rjdown
  • 9,162
  • 3
  • 32
  • 45
1

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();
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

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>\n<img src='img2.png' width='100' height='200'/><h1>Title 2</h1>\n<img src='img4.png' width='100' height='200'/><h1>Title 3</h1>"; 

preg_match_all($re, $str, $matches);
Twisty
  • 30,304
  • 2
  • 26
  • 45
0

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;
Mohammad
  • 3,449
  • 6
  • 48
  • 75
-2

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

http://php.net/substr

Cengiz Araz
  • 680
  • 9
  • 17