The Little Guy Posted October 28, 2010 Share Posted October 28, 2010 I currently have this line, which gets me all the links on a page. preg_match_all("~<a.+(href=\"(.+?)\")~i", $this->data, $arr); How can NOT include links in the array that contain the word javascript: at the beginning? Link to comment https://forums.phpfreaks.com/topic/217077-get-value-that-has-unless/ Share on other sites More sharing options...
Anti-Moronic Posted October 28, 2010 Share Posted October 28, 2010 I don't know of a way to do this *during* a regex, but you could simply iterate over your matches later and filter out those that include javascript. foreach($arr as $key => $element){ if(preg_match("#javascript#", $element)){ unset($arr[$key]); } } I'd love to know if this is possible during a capture. EDIT: actually, my regex above won't do because a url could contain javascript as a url to a page. You would have to amend to include : - and, from what I can tell, your regex will not match against urls which use ' to enclose the href. Link to comment https://forums.phpfreaks.com/topic/217077-get-value-that-has-unless/#findComment-1127415 Share on other sites More sharing options...
mattal999 Posted October 28, 2010 Share Posted October 28, 2010 Could just do this: foreach($arr as $key => $element) { if(stripos($element, "javascript:")) { // Or use strpos(strtolower($element), "javascript:") for PHP4 unset($arr[$key]); } } Link to comment https://forums.phpfreaks.com/topic/217077-get-value-that-has-unless/#findComment-1127516 Share on other sites More sharing options...
salathe Posted October 28, 2010 Share Posted October 28, 2010 Firstly, parsing HTML documents with regex is a really hacky way of doing things. There are tools dedicated to making sense out of (HT|X)ML documents built in to PHP, like DOM. Secondly, you can use a "negative lookahead" within your regular expression to assert that javascript: doesn't appear where you don't want it: ~<a.+(href="(?!javascript:)(.+?)")~i Link to comment https://forums.phpfreaks.com/topic/217077-get-value-that-has-unless/#findComment-1127522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.