1

i have a following pattern, inside the html file, that i would like to parse in php to get a link but for now i dont see a solution as i am trying to use QueryPath and my case is simply not a common dom element:

<script>
to.addVariable("site_name","http://www.sitename.com");
</script>

I just would like to return a link part of that pattern in order to print it.

Hope someone could recommend how to. Thank you.

UPDATE: I would like to get http://www.sitename.com as a value from the code above using php, maybe with phpQuery or QueryPath.

2
  • Could you rephrase your question, please? Commented Jan 23, 2011 at 5:41
  • I would like to get sitename.com as a value from the code above using php, maybe with phpQuery or QueryPath. Commented Jan 23, 2011 at 5:46

2 Answers 2

1

Something like this I guess will work

<?PHP
    $text = '
    <script>
    to.addVariable("site_name","http://www.sitename.com");
    </script>
    ';
    preg_match('#to\.addVariable\("site_name","([^"]+)"\);#', $text, $matches);
    echo $matches[1];
?>

You can also use preg_match_all if you have more than one to.addVariable(... strings in your <script> section.

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

Comments

0

Try this regular exp:

$regex = '#to\.addVariable\("(.+?)", "(.+?)"\)#';

Then, use preg_match_all to get the matches. If you want to check that the URL is an actual URL, the get any regular expression to match URLs and place it in the second .+?, these patterns will match anything between "", so you should check that you have what you need unless you trust the source.

NOTE: I'm not so sure that " doesn't needs to be escaped in regex, so check it out

Hope I can help! If you don't understand something drop a comment!

1 Comment

Would you kindly provide a small sample with preg_match and how to extract each var of the two (using result array indices 1 and 2) ? I think this would help the author.

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.