0

What is the way to get specific data using PHP. In this case i want to get some text which is wrapped by <span class="s"> to the first <b> HTML tag.
Assuming a HTML source code is:

Once there was a king <span class="s"> May 3  2009 <b> ABC Some Text </b> Some photo or video</span> but they have...

So, here i want to get those filtered data in a variable like: $fdata = "May 3 2009";
Because, May 3 2009 is wrapped by <span class="s"> to the first <b> HTML tag.
I will use it in SIMPLE PHP HTML DOM PARSING. So, any idea or example to filter those text and get it in a variable? Any idea will be a great help.

*If you found a duplicate question here, its not that its more specified.

2
  • Are you asking how to use simple html dom parser to grab the elements? Commented Nov 1, 2012 at 17:32
  • actually i can't understand how to filter those text and get in a variable, if its possible then i will able to play it with simple html dom parser. Commented Nov 1, 2012 at 17:34

2 Answers 2

2

Use Simple HTML DOM http://simplehtmldom.sourceforge.net/

Or http://php.net/manual/en/domdocument.loadhtml.php

Or you can use any other library also.

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

3 Comments

yeah thanks, but i know that. What i can't understand is how to get those text which wrapped by <span class="s"> to the first <b> HTML tag, and declare that in a variable, if u know n if possible please give me an example.
Hmm, if you don't want to figure out the code maybe check out: code.google.com/p/phpquery seems you could chain it like a jQuery selector if you are more comfortable with that.
is that possible to get that data by preg_replace, if so n if possible for you please show me a example. Thanks
1

If you're using simple html dom parser you'd grab the elements you're targeting like this:

$ret = $html->find('span class="s"'); 

This is just a basic sample, but it should get you going in the right direction.

if you need to find a very specific instance, you can use something such as:

$ret = $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;

3 Comments

actually i wanna get some filtered text in a variable, so we can compare it as getting a youtube video id from a url in a variable.
using my first example, it should return May 3 2009 <b> ABC Some Text </b> Some photo or video. Then you could trim everything after May 3 2009. You'll probably have to come up with some RegEx to handle this since the length of the month and day will probably vary.
thanks, i am trying with this but dont works: foreach($result->find('span class="s"') as $ftext) $new_des = implode(' ', array_slice(str_word_count($ftext, 1),0,4)); $result = str_replace($new_des, '', $result); $result = str_get_html($result); so it can delete first 4 word of span class="s", how to to make it work?

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.