The Little Guy Posted December 9, 2006 Share Posted December 9, 2006 OK... the code below takes all given text and turns it into a url... I works half way great.[code]<?phpif($string = preg_replace("~^/|^[a-zA-Z0-9.*$]~",$_POST["url"].'/$0',$nlink).'<br>'){ //code}?>[/code]If the url already has the http, It adds another one in front of it. So if I have this in the string:http://somesite.com/somethingcool/dog.php, when I run that function it changes it to:http://somesite.com/http://somesite.com/somethingcool/dog.phpHow can I make it so it doesn't do that, and leaves the text alone if it is already in a http:// format?I have another code:[code]<?phpforeach($links as $link){ $nlink = str_replace($replace, '',$link[0]); if($string = preg_replace("~^/|^[a-zA-Z0-9.*$]~",$_POST["url"].'/$0',$nlink).'<br>'){ $str = str_replace('//','/',$string); $str1 = str_replace('http:/','http://',$str); $str2 = explode("<br>",$str1);}}?>[/code]and right before it explodes the content, I would like it to ignore anything with an extension of .css, .js, image files, .exe, .zip. Basically all I want is HTML documents only so: HTML, HTM, PHP, ASP, etc. Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/ Share on other sites More sharing options...
desithugg Posted December 9, 2006 Share Posted December 9, 2006 Check the $_POST['url']; variable and remove any http:// thats already in there if any at all and than just have the code enter it.Something like the fallowing would take care of it.[code]<?$bfilter=array( ' ' => "%20",'http://' => "",);$post_url = str_replace(array_keys($bfilter), array_values($bfilter), $_POST['url']);if($string = preg_replace("~^/|^[a-zA-Z0-9.*$]~",$post_url.'/$0',$nlink).'<br>'){ //code}?>[/code][b][red]i think I misred what you posted.So ignore this[/red][/b] Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-137912 Share on other sites More sharing options...
The Little Guy Posted December 9, 2006 Author Share Posted December 9, 2006 Thanks. Now I have another question:How can I get this to get the url between the href=" "preg_match_all('~href=".*"~',$html,$links, PREG_SET_ORDER);This sometimes gets extra informationExample:Text it captures from: <a href="http://bill.nineplanets.org/offerings.html" style="color:#9999ff; font-size:9pt">Here is what it captures: http://bill.nineplanets.org/offerings.html%20style=color:#9999ff;%20font-size:9ptHere is what I want: http://bill.nineplanets.org/offerings.html Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-137918 Share on other sites More sharing options...
linuxdream Posted December 9, 2006 Share Posted December 9, 2006 Might want to look at parseurl()http://us3.php.net/manual/en/function.parse-url.php Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-137979 Share on other sites More sharing options...
The Little Guy Posted December 9, 2006 Author Share Posted December 9, 2006 I don't see how that will work, since I am getting more than just the URL, it is also giving me style too. so how would that get the URL out of it when there is also style?I realized that my code is getting anything that starts with href=" and going to the last occurrence of a " in the line within the code. I need it to get the contents between an href=" and the first occurrence of a " in the line within the code. Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138104 Share on other sites More sharing options...
linuxdream Posted December 10, 2006 Share Posted December 10, 2006 Maybe something like preg_match_all('/href="(.*?)"/, $html, $links); Makes the search not greedy and stops at the first " encountered after the URL. That way you have the link, then use parseurl() to breakup the URL into the pieces you wanted. Doing it the other way will match everything until the next href" in $html. Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138279 Share on other sites More sharing options...
The Little Guy Posted December 11, 2006 Author Share Posted December 11, 2006 [quote author=linuxdream link=topic=117930.msg481920#msg481920 date=1165716680]Maybe something like preg_match_all('/href="(.*?)"/, $html, $links); Makes the search not greedy and stops at the first " encountered after the URL. That way you have the link, then use parseurl() to breakup the URL into the pieces you wanted. Doing it the other way will match everything until the next href" in $html. [/quote]Thank You That works exactly. so... what does the ? mean? Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138756 Share on other sites More sharing options...
linuxdream Posted December 11, 2006 Share Posted December 11, 2006 The *? matches as few times as possible, otherwise called a lazy quantifier. So basically, with the " after the ? it will match everything upto the first " and stop. Without the ?, it would match all the way until the next full expression is matched, including any "'s Quote Link to comment https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.