-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
?>
kenny
  • 37
  • 5

2 Answers2

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

check
  • 559
  • 4
  • 12
  • can't imagine where you came up with that idea. –  Feb 26 '15 at 03:32
  • i just wanna help n solving. – check Feb 26 '15 at 03:36
  • 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
      , so it doesn't work. Thanks anyway.
    – kenny Feb 26 '15 at 04:17
  • whats the problem..? u can use json in .ajax to get return data form php side. – check Feb 26 '15 at 04:42
  • I don't know what the problem is. I can echo
      with ajax $("#get_user").text(data); , but when I use strip_tags, it echo nothing, if I put some text between
        , it will echo that text. I don't understand.
      – kenny Feb 26 '15 at 04:47
    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

    Community
    • 1
    • 1
    mjwunderlich
    • 1,025
    • 8
    • 13
    • you should never use Regular Expressions for parsing html. http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la –  Feb 26 '15 at 03:25
    • 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. – kenny Feb 26 '15 at 03:59
    • @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. http://stackoverflow.com/a/6752487/1000437 – mjwunderlich Feb 26 '15 at 14:51