0

I am trying to get /story.php?title=googlegoogle-google-google from the string $cool. The only way to get it is to use a multi-line regular expression. I tried the below code, but it didn't work. Can any one help me with this?

<?php 

    $cool ='<li><span class="sidebar-vote-number"><a href="/story.php?title=gamestarts now">1</a></span><span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span></li>

        Published News</a></div>
        </div>
        <div class="boxcontent">
            <ul class="sidebar-stories">

        <li><span class="sidebar-vote-number"><a href="/story.php?title=googlegoogle-google-google">1</a></span><span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span></li>';


    preg_match('/Published News<\/a><\/div>
        <\/div>
        <div class="boxcontent">
            <ul class="sidebar-stories">

        <li><span class="sidebar-vote-number"><a href="(.*?)"/msU', $cool, $match);


    echo $match[1];


        ?>  
Nick
  • 4,302
  • 2
  • 24
  • 38
Vishnu Simbu
  • 175
  • 1
  • 3
  • 13
  • 3
    [Why parse HTML with regex?](http://stackoverflow.com/a/1732454/1493698) – Antony Mar 11 '13 at 17:43
  • No other go, there are multiple similar syntax...I know about regex only..what else will help it ? – Vishnu Simbu Mar 11 '13 at 17:44
  • 1
    [Have you tried using an XML parser instead?](http://php.net/manual/en/class.domdocument.php) – Antony Mar 11 '13 at 17:45
  • No , I think i have achieved multiline html with similar regex by adding msU at the end....But this code does'nt seem to work – Vishnu Simbu Mar 11 '13 at 17:47
  • @Antony - It's really annoying when people reference that post. It's not an answer, it's a rant, and it only lives on because of "historical value". You [can parse HTML with regex](http://stackoverflow.com/a/4234491/211627), but it's not recommended. PHP in particular has a pretty terrible built-in HTML parser, but there are [many third-party parsers](http://stackoverflow.com/a/3577662/211627) which work well. – JDB Mar 11 '13 at 18:07
  • "Terrible" might be an exaggeration. "[Quirky](http://stackoverflow.com/q/4029341/211627) and steep learning curve" might be more accurate. – JDB Mar 11 '13 at 18:15

4 Answers4

3

What is wrong with

preg_match('~Published News.*<li><span class="sidebar-vote-number"><a href="([^"]*)~ms', $cool, $match);

? Make sure you use the correct variable $cool!

What do you know for sure? Which pattern?

powtac
  • 40,542
  • 28
  • 115
  • 170
  • Nice :) what is difference between ~ and / ,,just want to learn :) – Vishnu Simbu Mar 11 '13 at 17:51
  • 2
    I believe that will only find the first occurrence of the link. His example specifically pointed at the second occurrence. Although, it's not entirely clear if he only wants the second or wants both the first and second. – Brett Mar 11 '13 at 17:52
  • @Brett I see, changed my pattern. But also still unsure what IS the pattern. – powtac Mar 11 '13 at 17:53
  • @VishnuSimbu: Not much difference. Those are delimiter. Choosing `~` over `/` will remove the need to escape `/`. – nhahtdh Mar 11 '13 at 17:54
0

"Only way to get it is to use multi line regular expression." Oh no it isn't.

Apart from that, you are assiging the data to a variable called $cool but you are passing a variable called $game to the preg_match() function.

Captain Payalytic
  • 1,061
  • 8
  • 9
0
 preg_match('/Published News<\/a><\/div>
        <\/div>
        <div class="boxcontent">
            <ul class="sidebar-stories">

        <li><span class="sidebar-vote-number"><a href="(.*)">/msU', $cool, $match);

it works.

Curlas
  • 879
  • 5
  • 14
0

If your HTML string is semi-well formed you can use Xpath. http://codepad.org/vVBFNY7r

<?php
$cool ='<ul><li>
<span class="sidebar-vote-number"><a href="/story.php?title=gamestarts now">1</a></span>
<span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span>
</li></ul>';
$dom = new DOMDocument();
$dom->loadHTML($cool);

$xpath = new DOMXPath($dom);
$elements= $xpath->query('//a/@href');

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}

Outputs:

<br/>[href]/story.php?title=gamestarts now
<br/>[href]/story.php?title=googlegoogle-google-google
Matt
  • 5,315
  • 1
  • 30
  • 57