close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

preg_replace

Thread began 7/22/2013 2:48 pm by Jamie | Last modified 7/23/2013 11:47 am by Jamie | 552 views | 11 replies |

Jamie

preg_replace

Hi Jason

Had a premier support session with Ray the other evening re the mod_rewrite stuff from the Poerstore webinar.

he helped set up the database to duplicate the title column entry to a permalink one and use pre_replace on it.

This is what I am using in the update record

<?php echo ((isset($_POST["NewsTitle"]))?preg_replace('/[^0-9a-z]/i','-',$_POST["NewsTitle"]):""); ?>



What that is doing is taking this sort of title

A sweet marketing idea!

and changing it to

A-sweet-marketing-idea-

Its taking any replaced characters (ie ! or . etc) and replacing them with -

How would I keep spaces replaced with a - but just remove the chars I dont want completely?

Thanks

Jamie

Sign in to reply to this post

Jason ByrnesWebAssist

use str_replace to remove the special characters before the preg_replace replaces the slashes:
for example:

php:
<?php echo ((isset($_POST["NewsTitle"]))?preg_replace('/[^0-9a-z]/i','-',str_replace(array("!","?","@"),"",$_POST["NewsTitle"])):""); ?>
Sign in to reply to this post

Jamie

Thanks Jason

Am seeming to not be able to include things like ", # and ' to be removed... do I need to wrap them in a certain way?

Sign in to reply to this post

Jason ByrnesWebAssist

some character will need to be escaped, for example:
\"
\'

Sign in to reply to this post

Jamie

like this?

((isset($_POST["NewsTitle"]))?preg_replace('/[^0-9a-z]/i','-',str_replace(array("\"","\^","\[","\(","\)","\*","\+","\?","\\","\'","\.","\-","\!"),"",$_POST["NewsTitle"])):"")
Sign in to reply to this post

Jason ByrnesWebAssist

yes, that looks right.

Sign in to reply to this post

Jamie

cheers

Sign in to reply to this post

Jamie

Hey Jason

Why would the articles that have a ? in them fail to show?

http://www.ybg.org.uk/blog.php

The permalink is being changed in the _update page in the backend like this (not sure if that helps any):

?preg_replace('/[^0-9a-z()!?,]/i','-',str_replace(array("@","\-","/",".","\"","_","\'","*","#"),"",$_POST["BlogTitle"])):"")
Sign in to reply to this post

Jason ByrnesWebAssist

A question mark in a URL tells the server that anything after it is a URL variable.

for example:
results.php?var1=foo&var2=bar

the question mark tells the server that the information after it is variables, in this case a URL variable named var1 that has a value of foo and another named var2 that has a value of bar.

Sign in to reply to this post

Jamie

Ok, I get that... thanks

What if its part of a url friendly sentence, ie Credit control out of control? being converted to Credit-control-out-of-control? so that the resulting URL would be www.somedomain.com/blog/Credit-control-out-of-control?

If there is nothing after the ? .. why wouldnt that show?

Sorry, am not questioning you, am just looking to understand for my future ref.....

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...