1

My code is given below:-

$text = "<div class='title'>Title</div><div class='content'>This is title</div>";

$words = array('Title');

$words = join("|", $words);

$matches = array();

if ( preg_match('/' . $words . '/i', $text, $matches) ){
    echo "Words matched: <br/>";
    print_r($matches);
}

else{
    echo "Not match";
}

The problem is that in above code I am finding title but i don't want to print title; I want to print this: "This is title" and I am not understanding how I can print this by finding title.

Because title is like keyword that will not change but value which i want to print it is dynamic value and it will change every time, that's why i cannot finding value of title. So how can i do it?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Learner
  • 177
  • 2
  • 15
  • 3
    Don't use regexes. Use [DOM](http://php.net/dom) – Marc B Dec 03 '13 at 18:19
  • Are you trying to parse an external HTML file? – Rwd Dec 03 '13 at 18:19
  • Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) instead. – Madara's Ghost Dec 03 '13 at 18:21
  • Yes, I want to parse external HTML file. Can you please explain how can i do it? Because i am a learner. – Learner Dec 03 '13 at 18:22

1 Answers1

0

Don't use regex for parsing HTML. Use a DOM Parser instead. In this case, you can use an XPath expression to get the element by class name:

$text = "<div class='title'>Title</div>
<div class='content'>This is title</div>";
$dom = new DOMDocument;
$dom->loadHTML($text);
$xpath = new DOMXPath($dom);
$title = $xpath->query('//*[@class="content"]')->item(0)->nodeValue;

Output:

This is title

This should get you started. If the title is in a different position, you can modify the expression accordingly to retrieve it.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I have one more question, If i have multiple keys and don't have class, so how can i do it? Because some time it's happining that there is not mention class and if there is class so class name is different than before. – Learner Dec 03 '13 at 18:27
  • @user2965171: What do you mean *multiple keys*? – Amal Murali Dec 03 '13 at 18:29
  • If this is come than? -->$text = "
    Title
    This is title
    Value
    This is value
    Keyword
    This is keyword
    "; $words = array('Title','Value','Keyword');
    – Learner Dec 03 '13 at 18:45
  • In this example which i send before there is not any class then how can i find? – Learner Dec 03 '13 at 18:46
  • @user2965171: How do you know which one is the title? Does it have a unique property? Is it always the second `
    `? Without details (and clear explanation), I don't think I'll be able to help you.
    – Amal Murali Dec 03 '13 at 18:48
  • I know that first one is title and second is that's value and title will be always same but value is dynamic that will keep changing. Value will always without detail. – Learner Dec 03 '13 at 18:55
  • @user2965171: In that case, you can simply use `$title = $dom->getElementsByTagName('div')->item(1)->nodeValue;`. [See demo.](https://eval.in/75576) – Amal Murali Dec 03 '13 at 19:14
  • Amal Murali You are awesome programmer. Whatever you told me every thing is working. I have one more question and that is last because last condition is remainig that condition is that i have a html page and that page is this page:- "https://play.google.com/store/apps/details?id=com.runtastic.android.pro2" please visit on this page and now i would like to ask you that i want to find some titles and print values from this page. Those keys are these (1)Updated (2)Size (3)Installs (4)Current Version (5)Requires Android (6)Content Rating these keys values i would like to find and i have 20 pages. – Learner Dec 03 '13 at 19:31
  • Like this page, but problem is that i don't know which one page contain class in value or which one page not, and also it is possible that some keys and values keeps class and some not, and some keeps different class than other key So this is my last question know how can i find know? I have to find just these Key's Values which i mentioned above. – Learner Dec 03 '13 at 19:38