homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Sentences into URLs
How to convert?
madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3207539 posted 2:40 am on Jan 4, 2007 (gmt 0)

Hey all,

I would like to take some data, say a string like "This is my site" and use php to take that and turn it into "this-is-my-site" for example.

Is there a function that does this? I assume I would store the string in a variable, like $txt="this is my site" and then output the new string into another variable.

If anyone can point me towards a function that will do this, that'd be great. I'm obviously a noob at php :)

 

mcavic

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3207539 posted 2:44 am on Jan 4, 2007 (gmt 0)

preg_replace() [us2.php.net]

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3207539 posted 2:45 am on Jan 4, 2007 (gmt 0)

I'm confused as to what you mean as a URL. I do not understand what you are getting at with your example.

Do you mean something like this?

$text = "example.com";
echo '<a href="http://www'.$text.'">'.$text.'</a>';

Or do you mean that you want to transform specific words in a body of text into a link by searching for them? (something maybe with preg_replace)

Can you please specify if I'm thinking incorrectly?

;)

Sorry for my misunderstanding

TravH

5+ Year Member



 
Msg#: 3207539 posted 3:18 am on Jan 4, 2007 (gmt 0)

You could also use: urlencode('Your sentence here.')
If you were trying to make sentences with symbols and spaces into valid urls.

madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3207539 posted 3:25 am on Jan 4, 2007 (gmt 0)

Thanks for the responses.

I'm trying to take a sentence such as "my sentence is this" and be able to use it as a url like "my-sentence-is-this.html"

I think I may have found a different way of doing this, based on some variables in the script. For example one variable returns the filename "image.jpg". I'd like to take that and strip off the .jpg.

Would I do something like:
$filename = image.jpg
$new_url = preg_replace ($filename)

?

I know I'm missing a step. Do I need to explode the $filename variable?

madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3207539 posted 3:26 am on Jan 4, 2007 (gmt 0)

Whoah - urlencode looks very interesting too. I'm gonna test that out.

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3207539 posted 3:30 am on Jan 4, 2007 (gmt 0)

>>>I'd like to take that and strip off the .jpg

You can take a look at basename [us3.php.net] however if I remember correctly the suffix is case-sensitive.

Jordo needs a drink

5+ Year Member



 
Msg#: 3207539 posted 3:40 am on Jan 4, 2007 (gmt 0)

Whoah - urlencode looks very interesting too. I'm gonna test that out.

It can make your url look pretty ugly...

If you want clean looking urls with dashes instead of %20 then use preg_replace to change spaces to dashes.

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3207539 posted 3:42 am on Jan 4, 2007 (gmt 0)

>>>then use preg_replace to change spaces to dashes

For something like this you are better off using str_replace [us2.php.net] ;)

Jordo needs a drink

5+ Year Member



 
Msg#: 3207539 posted 3:47 am on Jan 4, 2007 (gmt 0)

For something like this you are better off using str_replace ;)

You're right... I was just sticking with the theme... lol

madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3207539 posted 5:33 am on Jan 4, 2007 (gmt 0)

Talk about more than one way to skin a cat.

Alright I'll play around with all these options and see what I can get going :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved