ytse_jam
ytse_jam

Reputation: 185

how to drop base64 img string

I'm pulling a content from a table using php with several text including a base64 img string, i would like to drop or remove the base64 string.

By the way, the text and base64 img is coming from summernote wysiwyg editor.

hope you can help me about this. thanks in advance.

<p>several text here </p>
<p>several text again here </p>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4AeydCXxU1dn/f5M9IQvLANnYt6BCIhI3xC1xC1WwUAvVgvJi3742L69ibRH/1XQD1AKWptYqBaFakYriQkRNBAQKGM==" data-filename="image.png" style="width: 1078px;"><br></p>

Upvotes: 0

Views: 613

Answers (1)

Andreas
Andreas

Reputation: 23968

You can use strip_tags().
Here I use the second parameter to allow <p> tag but if the actual tag is not important remove the "<p>" from the function.

$html = '<p>several text here </p>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4AeydCXxU1dn/f5M9IQvLANnYt6BCIhI3xC1xC1WwUAvVgvJi3742L69ibRH/1XQD1AKWptYqBaFakYriQkRNBAQKGM==" data-filename="image.png" style="width: 1078px;"><br></p>';

Echo strip_tags($html, "<p>");

Output:

<p>several text here </p>
<p></p>

But as I said, if it's only the text that is needed just remove the "<p>" and it will only echo the actual text.

If you only want the first line and with the tag you can explode on PHP_EOL and use the first item.

Echo explode(PHP_EOL, strip_tags($html, "<p>"))[0];

output:

<p>several text here </p>

Upvotes: 2

Related Questions