[SOLVED] preg_replace from URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zolee1
Forum Newbie
Posts: 7
Joined: Wed Oct 20, 2004 12:02 am
Location: Far FarAway

[SOLVED] preg_replace from URL

Post by zolee1 »

Hi,

I am fairly new to PHP.
I would like to crate FROM $url = "http://www.a1-whatever-4u.com/";
this string: A1-whatever-4u.com
The code bellow produces A1-whatever-4u.com/.
I can't get rid of the '/' at the end.
Also I would like to make sure that if
I have a URL starting with a digit that I wouldn't get an error.
With the current code I might get, because it would try to capitalize the first character after the www.

Code: Select all

<?php
$url = "http://www.a1-whatever-4u.com/";

$replacement = "strtoupper('\$4').'\$5'";
echo preg_replace("/(http:)(\/\/)(www.)([a-zA-Z0-9])([a-zA-Z0-9])+([\/])?/e", 
             $replacement, 
             $url);
?>

Any suggestions would be appreciated.

Zolee
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're very close..

Code: Select all

<?php

	$url = "http://www.a1-whatever-4u.com/";
	
	$replacement = "strtoupper('\\\\2').'\\\\3'";
	echo preg_replace("#http://(www\.)?([a-z0-9])([a-z0-9_\.-]+)(/.*)?$#ieS", 
		$replacement, 
		$url);

?>
zolee1
Forum Newbie
Posts: 7
Joined: Wed Oct 20, 2004 12:02 am
Location: Far FarAway

Post by zolee1 »

Thanks feyd,

But I get the same result as with my code:
A1-whatever-4u.com/

Do you know how can I loose the / at the end, so I would get only this:

A1-whatever-4u.com

Thanks,
Zolee
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mine works correctly. Consider switching to preg_match.. your original pattern doesn't match the entire string.
zolee1
Forum Newbie
Posts: 7
Joined: Wed Oct 20, 2004 12:02 am
Location: Far FarAway

Post by zolee1 »

Hi Feyd,

Maybe I did something wrong, but it didn't work. I modified your code:

Code: Select all

<?php

$url = "http://www.a1-whatever-4u.com/";

echo preg_replace("#http://(www\.)?([a-z0-9])([a-z0-9_\.-]+)(/.*)?$#ieS", 
             "strtoupper('\$2').'\$3'",
             $url);

?>
and it works now!!!

Thank You!!!

Do you know any good place on the net where I can find explanation of the syntax of regular expressions.

I know the basic stuff, but I don't know what that means at the end in your code : #ieS

Thanks Again!

Zolee
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

zolee1
Forum Newbie
Posts: 7
Joined: Wed Oct 20, 2004 12:02 am
Location: Far FarAway

Post by zolee1 »

Thanks again!

It was very helpful.

Zolee
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Amazing, I log on to ask this very question, and what do I get? The code already laid out for me! w00tsies.
Post Reply