Shoutbox

php, preg replace help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: php, preg replace help (/showthread.php?tid=52138)

php, preg replace help by Ezra on 10-23-2005 at 11:17 PM

I'm making a bookmarks feature for my website, and I got the cookie system and all already working, but when users forget to put a http:// before the link, it will point to my server and make the link useless.

Now I want to make a system that checks the links before they get submitted, and add a http:// if it's missing.

Now i'm thinking that I can use preg replace to do this, but I really don't understand how to use it.

If someone can make a simple sample for me and show me a good tutorial to use preg replace I can get on with my project :).

The php.net page about the function is really lacking good explanation.

TIA


RE: php, preg replace help by KeyStorm on 10-23-2005 at 11:23 PM

You better use substrings for that... tbh


RE: php, preg replace help by Ezra on 10-23-2005 at 11:24 PM

substrings? I'm sorry i'm a php n00b :P


RE: php, preg replace help by Stigmata on 10-23-2005 at 11:24 PM

psuedo..

code:
if left(URL, 7) != "http://" then
URL = "http://" & URL
end if


thats how i would do it...

in php thats something like..

code:
$pos = strpos($URL, "http://");

if ($pos === false) {
   $URL = "http://". $URL . "";
} else {
   echo "its there :D";
}


RE: php, preg replace help by Ezra on 10-23-2005 at 11:32 PM

Ah, thanks a bunch :)

I'm trying substr and that idea of stigmata :)

EDIT: OMGWTFBBQ

I managed to break my script :S

It's gonna take some time to get it working again, but I believe your script is working stigmata, thanks for that.


RE: php, preg replace help by brian on 10-24-2005 at 01:10 AM

If you www.pastebin.com'd it.. we could help.


RE: php, preg replace help by Ezra on 10-24-2005 at 12:47 PM

nah, already found it, I made a function and used a return but that caused the rest of the function to stop executing, so I changed it a little :)

And that code from stigmata works great :)


RE: php, preg replace help by Stigmata on 10-24-2005 at 01:05 PM

:banana:

good to know i can still code :)


RE: php, preg replace help by J-Thread on 10-24-2005 at 01:24 PM

What if I enter this link:

https://www.thisisasafesite.com

It'll be:

http://https://www.thisisasafesite.com

You'd better do:

code:
if (!preg_match("/^[a-z]+:\/\//i", $URL) {
   $URL = "http://". $URL;
}

Should be something like that I think...
RE: php, preg replace help by Ezra on 10-24-2005 at 02:28 PM

I guess that will also allow me to use other protocols like ftp, thats good :)

But could you gice me something that explains that "/^[a-z]+:\/\//i"
So I don't have to ask for it next time :D


RE: php, preg replace help by WDZ on 10-24-2005 at 02:32 PM

quote:
Originally posted by Ezra
But could you gice me something that explains that "/^[a-z]+:\/\//i"
http://php.net/reference.pcre.pattern.syntax

It will match any string that begins with 1 or more letters followed by a colon and 2 slashes. It's also case-insensitive. :p
RE: php, preg replace help by J-Thread on 10-24-2005 at 03:00 PM

Exactly(Y)

I'm not sure if there are protocols with other chars then a-z in it, but it supports at least http, https, ftp etc. And if you ever see another protocol, you can always change it;)

Btw:
"/^[a-z]+:\/\//i"

The "main preg" is between / and /
The ^ stands for "begins with";
[a-z]+ is one of more letters
: is a colon
\/ is a /, but should be escaped with a \
\/ is another one
/ is the last / of the preg
i means case insensitive:D


RE: php, preg replace help by Ezra on 10-24-2005 at 03:27 PM

Thanks for the explanation both :)