0

We have variable $menu with HTML inside (there is no loop, it comes from a function).

On echo it gives the code like this:

<ul id="menu">
    <li id="some-id" class="many classes one"><a href="#">text</a></li>
    <li id="some-id" class="many classes second active"><a href="#">text</a></li>
    <li id="some-id" class="many classes three"><a href="#">text</a></li>
</ul>

What I want to do:

  1. get value of the class="" of each <li>.

  2. if active exists in this value, then go to 3) step.

  3. search for one, two, three, four, five inside value. If one of them exists, then throw its name to php variable.

Variable $menu should give:

$name = 'two';

What is the solution?

DisplayName
  • 3,093
  • 5
  • 35
  • 42
James
  • 42,081
  • 53
  • 136
  • 161
  • What’s the difference to [Move title attribute value to class attribute value in the HTML code](http://stackoverflow.com/questions/4007569/move-title-attribute-value-to-class-attribute-value-in-the-html-code)? – Gumbo Oct 24 '10 at 15:03
  • This seems really convoluted. What's the ultimate goal here? – Brad Mace Oct 24 '10 at 15:07
  • @Gumbo, this question looks like that, but it's different. – James Oct 24 '10 at 15:15
  • Yes, it would seem much simpler to have the function pass back the value you want than to encase it in HTML cruft and then try to scrape the original value back out of the HTML. – bobince Oct 24 '10 at 15:15
  • @bobince, code comes from the root function of cms (it can't be changed). – James Oct 24 '10 at 15:17
  • 1
    @Happy: In what way is it different to [Move title attribute value to class attribute value in the HTML code](http://stackoverflow.com/questions/4007569/move-title-attribute-value-to-class-attribute-value-in-the-html-code) (sorry, had liked the wrong question)? You want to extract some HTML attribute value – just like in the other question. – Gumbo Oct 24 '10 at 15:19
  • @Happy This is your third question that asks about altering the output of the function generating the Main Menu. Why dont you just change the function? What CMS are you using? What is the name and code of the core function? – Gordon Oct 24 '10 at 15:35
  • @Gordon, its a core function (deep inside cms), I can't change it. – James Oct 26 '10 at 08:53
  • @Happy the function being a *"core function (deep inside CMS)"* is not a reason **why** you cannot change it. PHP scripts are usually not compiled, so you can very much change everything. – Gordon Oct 26 '10 at 09:13

4 Answers4

3

Use an XPath query.

see here:http://php.net/manual/en/domxpath.query.php

ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • @Happy I [already gave you all the example you need to solve your problem](http://stackoverflow.com/questions/3958403/catch-first-and-last-li-inside-php-variable/3958493#3958493). Why not actually go the PHP Manual and learn how to use DOM instead of expecting people to provide you the codes you can copy and paste. Dont ask for a fish. Learn how to fish. – Gordon Oct 24 '10 at 15:42
1
$html = '<ul id="menu">
    <li id="some-id" class="many classes one"><a href="#">text</a></li>
    <li id="some-id" class="many classes two active"><a href="#">text</a></li>
    <li id="some-id" class="many classes three"><a href="#">text</a></li>
</ul>';

$active = 'active';
$valid = array('one','two','three','four','five');

$x = simplexml_load_string($html);

foreach($x->xpath('//ul/li[contains(@class,'.$active.')]') as $li)
{
    if($common = array_intersect($valid, explode(' ',$li->attributes()->class)))
    {
        $menu = array_shift($common);
        break;
    }
}
echo $menu;
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
0

You should use Regular Expressions.

Or if you can catch each Li item classes in the previous functions might be easier.

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • 1
    You should park your car with a stone under your tire. But you also could just juse something the car has already made for that purpose like a handbrake(XPath for querying xml documents). – ITroubs Oct 24 '10 at 15:08
  • what's wrong with regex? It takes much memory to handle, or much time, or what else, why you hate it? – James Oct 24 '10 at 15:14
0

I think you should wrap the solution into a function.

Regular Expressions fit for the job, but I think you can also use DOM class. Something like:

$menu = '<ul id="menu">
    <li id="some-id" class="many classes one"><a href="#">text</a></li>
    <li id="some-id" class="many classes two active"><a href="#">text</a></li>
    <li id="some-id" class="many classes three"><a href="#">text</a></li>
</ul>';
// using a constant instead a "magic number" inside below function
define(NUMBER_POSITION, 2);
function getActiveItem($menuStr) {
    $doc = new DOMDocument();
    $doc->loadXML($menuStr);
    $listItems = $doc->getElementsByTagName('li');
    foreach($listItems as $listItem) {
        // case count equals 1, expression will be true
        if (substr_count($listItem->getAttribute('class'), 'active')) {
            $classes = explode (' ',$listItem->getAttribute('class'));
            return $classes[NUMBER_POSITION];
        }
    }
}

echo getActiveItem($menu);

That's it.

Davis Peixoto
  • 5,695
  • 2
  • 23
  • 35