4

I have a long string of HTML that contains

<p>
<img>
<span> 

and a bunch of other tags.

Is there anyway of extracting ONLY the text within the tags from this string?

2 Answers 2

10

If you want to extract all text within any tags, the simple way is to strip the tags: strip_tags()

If you want to remove specific tags, maybe this SO questions helps.

Sign up to request clarification or add additional context in comments.

Comments

1

I know I'll be getting a lot of bashing for this, but for a simple task like this I'd use regular expressions.

preg_match_all('~(<span>(.*?)</span>)~', $html, $matches);

$matches[0] will contain all the span tags and their contents, $matches[1] contains only the contents.

For more complicated stuff you might want to take a look at PHP Simple HTML DOM Parser or similar:

// Create DOM from URL or file
$html = str_get_html($html);

// Find all images
foreach($html->find('img') as $element) {
   echo $element->src . '<br>';
}

Etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.