Jump to content

Using preg_replace

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
In the string $GetName, I want to remove all "~" characters and any words "None".

In the code below, I can get the results I want. My question is, can I do the same thing using only one preg_replace?



$GetName =  "Florence~Pauline~Smith~Johnson~None~1915-03-06~2000-04-03";

$MotherName = preg_replace("/~/"," ", $GetName);

$MotherName = preg_replace("/None/"," ", $MotherName);

Echo "Mother: $MotherName</br>"; 

//Mother: Florence Pauline Smith Johnson 1915-03-06 2000-04-03




#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Actually, you should be using str_replace() for these functions. Unless you are going to use a regular expressions preg_replace is always slower than str_replace for string manipulation.

$GetName =  "Florence~Pauline~Smith~Johnson~None~1915-03-06~2000-04-03";
$MotherName = str_replace("/~/"," ", $GetName);
$MotherName = str_replace("/None/"," ", $MotherName);
Echo "Mother: $MotherName</br>";  


#3
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Thanks for the info on using str_replace() rather than preg_replace.

I actuall figured out how to return an array from a function. That makes it so much easier to do what I wanted.

Thanks,
Jon

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I'm guessing you used explode or split?

#5
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
No, I didn't use explore or split. I Googled my problem and found the answer out there somewhere. It is a very simple solution but I don't understand how/why it's working. I'm going to start a new thread "Returning an array from a function" with the code to see if you can help me answer my question. I will post it in then next couple of days.

Jon

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Sounds good. I'll be glad to help.