Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2012, 11:12 AM   PM User | #1
kingot
Regular Coder

 
Join Date: Feb 2011
Posts: 114
Thanks: 12
Thanked 0 Times in 0 Posts
kingot is an unknown quantity at this point
Help me on preg_replace i'm working on

Hi,
Please i'm writing a program using a regular expression .
Now , i created a string called 'this is a test' and i want to replace 'test' with 'assignment', i try this code but not working , can someone help me
Here is the code


Code:
[<?php] 
   $string='this is a test';
  $preg_replace= preg_replace('/[a-zA-Z]/','assignment', $string);
   echo $preg_replace;
   
[ ?>]
Thanks
Clement Osei
kingot is offline   Reply With Quote
Old 01-10-2012, 01:05 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
No reason to use preg_replace for a simple replacement like that.

PHP Code:
$string='this is a test';
echo 
str_replace('test','assignment',$string); 
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-10-2012, 01:20 PM   PM User | #3
kingot
Regular Coder

 
Join Date: Feb 2011
Posts: 114
Thanks: 12
Thanked 0 Times in 0 Posts
kingot is an unknown quantity at this point
Hi Sir,,
Thanks very much for your reply.
Well , i do know how to use the str_replace but want to know using preg_replace.

Please can you help me out ,using the example above.

Thanks Much.

Clement Osei
kingot is offline   Reply With Quote
Old 01-10-2012, 01:51 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
For simple ones like your example, str_replace is a LOT faster (processing wise).

Regular expressions should only be used to match patterns - something you don't know specifically the value of, or a pattern you need to replace (like parsing BB code).

In other words, learn how to use it with its proper designed application.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 01-10-2012, 01:56 PM   PM User | #5
kingot
Regular Coder

 
Join Date: Feb 2011
Posts: 114
Thanks: 12
Thanked 0 Times in 0 Posts
kingot is an unknown quantity at this point
Hi Sir,
Thanks for your advise.
I really appreciate it..!!

Clement Ose1
kingot is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.