0

there have some links, some are in javascript code. how to preg match there urls? (the urls could be end by .shtml, .html, / or .jsp, so I think regex them in single quotes, but nothing return.)

$sitelink = "javascript:pre('http://www.domain.com/cotagory/articles/2012/09/23/notice.shtml',%20%,500)";
//$sitelink = "javascript:box('http://www.domain.com/cotagory/articles/2012/09/23/sports/',%18%,500)";
if(strpos($sitelink,'javascript')===true){
    preg_match_all("@'(.*)'@i",$sitelink,$matches);
    var_dump($matches);
}

2 Answers 2

1

Change the if condition to:

if(strpos($sitelink,'javascript') !== false){

The position of 'javascript' within $sitelink is 0 that is not true!!!

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

2 Comments

This will always yield true because strpos() never returns -1
Still think matching 'javascript' at the start of the string is better, but since OP never stated this behaviour explicitly I'll revert the DV
1

The logic of matching 'javascript' at the start of the string is wrong; it should be:

strpos($sitelink, 'javascript') === 0

The regular expression could also be a little nicer:

preg_match("@'([^']*)'@", $sitelink, $matches);

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.