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?

9
  • What’s the difference to Move title attribute value to class attribute value in the HTML code? Commented Oct 24, 2010 at 15:03
  • This seems really convoluted. What's the ultimate goal here? Commented Oct 24, 2010 at 15:07
  • @Gumbo, this question looks like that, but it's different. Commented Oct 24, 2010 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. Commented Oct 24, 2010 at 15:15
  • 1
    @Happy: In what way is it different to 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. Commented Oct 24, 2010 at 15:19

4 Answers 4

3

Use an XPath query.

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

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

1 Comment

@Happy I already gave you all the example you need to solve your problem. 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.
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;

Comments

0

You should use Regular Expressions.

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

2 Comments

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).
what's wrong with regex? It takes much memory to handle, or much time, or what else, why you hate it?
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.

Comments

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.