0

I am using the SimpleHTMLDomParser to go through a html code and find various things. Everything works fine so far, but there is one problem:

How do I find a string that has no ID, no class and no unique parent element?

In my case, I started with extracting content from a div:

$descrs    = $html->find('.show_synopsis');

foreach($descrs as $descr) { 

    echo($descr->innertext);

}

This looks like:

<div class="show_synopsis">

    Lorem ipsum dolor sit amet, consetetur sadipscing elitr. <b>Source:</b> LORES.

</div>

Now, is it possible find and delete LORES from the above example?

Since LORES is a variable and can change, I was wondering if its possible to simply always find the word next to <b>Source:</b>?

I have tried a few different ways, but none worked so far. I have tried to adapt a solution from this post, but wasnt able to adjust them for my needs.

2 Answers 2

2

Try this:

echo preg_replace('/(.?)<b>.*Source:.*<\/b>.*\./', '$1', $descr->innertext);
Sign up to request clarification or add additional context in comments.

4 Comments

This seems not to work. Although I get no error message, the <b> Source: </b> LORES gets "echo´ed" as-well and is still visible in the output!
You first said <b>Source:</b> and then <b> Source: </b> (spaces before and after Source:); which one is the one you need the replace?
Sorry for the confusion. It looks exactly like this: Random text.<br><br><b>Source: </b>CBS <br>Random text.
I just edited my answer to cover both with and without spaces scenarios. Please try it again.
1

Can't you just replace the LORES in the string you are echo-ing?

echo str_replace('LORES', '', $descr->innertext);

6 Comments

Oh yeah :) That seems to work. One last thing: if I use this way and have various strings, would I simply create a for each loop and check each string or is there another (simpler) solution?
You can replace 'LORES' needle in the replace function with an array of multiple needles.
Oh and if this matters: the html always looks like this: <b>Source:</b> LORES - so would i even be possible to always remove <b>Source:</b> and the word (LORES in our example) next to it?
I have modified my question a bit, but overall your solution works fine - but it would be more comfortable if I could find a way without having to know all possible strings and puting them in an array.
In this case you could use preg_replace instead of str_replace.
|

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.