-1

I am trying to explode 'ul' to get the text. Code below, Help, appreciate.

<?php
$html="<ul id='get_user' class='get_user'>1,2,3,4</ul>";
$first_explode=("<ul id='get_user' class='get_user'>",$html);
$second_explode=("</ul>", first_explode[1]);
$thrid_explode=(",", $second_explode[0]);
echo $thrid_explode[1]; //expecting 2
?>

4
  • is that the whole real string? Commented Feb 26, 2015 at 2:58
  • Have you heard of this new thing called Regular Expressions? Commented Feb 26, 2015 at 2:59
  • what is Regular Expressions? Commented Feb 26, 2015 at 3:00
  • if it is all as you show above, all you need is php.net/strip_tags Commented Feb 26, 2015 at 3:14

2 Answers 2

1

just with this.

<?php
    $html ="<ul id='get_user' class='get_user'>1,2,3,4</ul>";
    $arrayUL = explode(",", strip_tags($html));
    print_r($arrayUL);
?>

strip_tags Manual

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

5 Comments

can't imagine where you came up with that idea.
i just wanna help n solving.
Thanks for your help. You solve my question. but unfortunately, I cannot solve my problem. Because I use .ajax to return the text to a <ul>, so it doesn't work. Thanks anyway.
whats the problem..? u can use json in .ajax to get return data form php side.
I don't know what the problem is. I can echo <ul></u> with ajax $("#get_user").text(data); , but when I use strip_tags, it echo nothing, if I put some text between<ul></ul>, it will echo that text. I don't understand.
0

Check out this Stackoverflow answer: RegEx to extract text between a HTML tag

The regex "/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/" will extract text within HTML tags. You can use php's preg_match: http://php.net/manual/en/function.preg-match.php

Like so:

$regex = '/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/';
$string = '<div class="some-class" style="display:block;">Hello</div>';
preg_match($regex, $string, $matches);
print_r($matches);

In summary, you want to use a Regular Expressions for parsing anything more complicated than simple character delimiters. Here's a handy online regular expression tester: https://regex101.com/#pcre

3 Comments

you should never use Regular Expressions for parsing html. stackoverflow.com/questions/6751105/…
Thanks for you time. But I copy and paste your code exactly to test. It doesn't work. Did you miss something or probably I don't know how to use it.
@Dagon it's a matter of usage -- if it's a single line of HTML you need to parse, it works quite well. Obviously for larger endeavors, use an XML DOM parser. stackoverflow.com/a/6752487/1000437

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.