Jump to content

Recommended Posts

OK... the code below takes all given text and turns it into a url... I works half way great.
[code]<?php
if($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.php

How 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]<?php
foreach($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.
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-137912
Share on other sites

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 information
Example:

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:9pt
Here is what I want: http://bill.nineplanets.org/offerings.html
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-137918
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138104
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138279
Share on other sites

[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?
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138756
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/30000-preg_replace-ignore/#findComment-138779
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.