1

I have blocks of divs that have anchor tags inside.

How can you extract the value of the property "sid" from the href property of each anchor tag?

the way i get my code data :

$.getJSON('http://anyorigin.com/get?url=http://www.somesite.com&callback=?', function(data){
        //$('#output').html(data.contents);


 var code = data.contents;

 document.myform.outputtext.value = code

I tried this but it doesn't output value of href ?

var pattern = /<a href="([^"]+?)">/gi;
code = code.match(pattern);
for (i = 0; i < code.length; i++) {
document.write(code[i].replace(pattern, '<a href="./doit.php?Id=$1&title=$2">$2</a><br />'));
}

example of code string:

<td width=120 valign="top">                     <div style="height:135px;
    border:1px solid #BBBBBB; background:#BBBBBB; margin-left:2px;
    text-align:center; ">
                    <a href="/now/episodes.php?name=path&id=4000&sid=12345&page=0"><img
    border="0" src="http://www.somesite.com/1234.jpg" width="150"
    height="83"></a><br>
                        <font face="Tahoma" size="2"><b>Star Album</b><br/>
                            episode 4
                        </font>             </div>


        </td>
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • I suggest first looking at this question: http://stackoverflow.com/questions/7266631/selecting-elements-without-jquery – knownasilya Dec 10 '12 at 20:41

2 Answers2

1

Try:

var url="/now/episodes.php?name=path&id=4000&sid=12345&page=0"

var match = url.match(/sid=(.*)\&/);
alert( match[1] );​
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
0
<?php
$referenceStr = "<td width=120 valign=\"top\">                     <div style=\"height:135px;
    border:1px solid #BBBBBB; background:#BBBBBB; margin-left:2px;
    text-align:center; \">
                    <a href=\"/now/episodes.php?name=path&id=4000&sid=12345&page=0\"><img
    border=\"0\" src=\"http://www.somesite.com/1234.jpg\" width=\"150\"
    height=\"83\"></a><br>
                        <font face=\"Tahoma\" size=\"2\"><b>Star Album</b><br/>
                            episode 4
                        </font>             </div>


        </td>";

$key = 'sid';   
preg_match('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', $referenceStr,$matches);
foreach($matches as $k => $v) $matches[$k] = str_replace(array('&sid=','sid='),'',$v);
print_r($matches);
?>

Here is a PHP solution, just use the pattern, it will work for JS too.

For JS :

var code = '<td width=120 valign="top"> <div style="height:135px; border:1px solid #BBBBBB; background:#BBBBBB; margin-left:2px; text-align:center; "> <a href="/now/episodes.php?name=path&id=4000&sid=12345&page=0"><img border="0" src="http://www.somesite.com/1234.jpg" width="150" height="83"></a><br> <font face="Tahoma" size="2"><b>Star Album</b><br/> episode 4 </font> </div> </td>';
matchingelements = code.match(/sid\=*(\d*)/ig);
for(i=0;i<code.length;i++){
    document.write(matchingelements[i].replace('&sid=','').replace('sid=',''));
}

This outputs 12345

Graben
  • 202
  • 3
  • 8
  • thanks for your code . is this correct pattern to use ?var pattern = /(?:&|(\?))sid=[^&]*(?(1)&|)?/i; – user1788736 Dec 10 '12 at 20:56
  • 1
    /sid\=*(\d*)/ig or /(?:&|(\?))sid=[^&]*(?(1)&|)?/ig Both seems to work when tested on your HTML source example. – Graben Dec 10 '12 at 21:27
  • do not forget /ig at the end where g mean to get all occurences in JS – Graben Dec 10 '12 at 21:28
  • graden it seems i cant run the suggest pattern. could you show me an example in how to run that pattern using javascript so it give me sid= and episode number and episode name ? – user1788736 Dec 10 '12 at 23:27
  • I updated my comment to get the sid parameter in JS using your HTML code string you provided as the base for testing the code. – Graben Dec 11 '12 at 00:52
  • Many Many thanks garben. i am getting my code data from $.getJSON and it is not in one line as your example so i still i just can use your updated code just after it ? – user1788736 Dec 11 '12 at 01:23
  • Ya... I just removed those line breaks so it was more compact... but it wont change anything... – Graben Dec 11 '12 at 03:10
  • You should close this question if it is resolved, as I believe I answered it correctly. – Graben Feb 17 '14 at 19:32