Author Topic: Replacing Strings!  (Read 1152 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Replacing Strings!
« on: November 01, 2006, 02:26:47 PM »
This function is very useful. It's called str_replace(). It finds a specified string and replaces it with another in a source string.

Code: [Select]
<?php
echo str_replace("world","Peter","Hello world!");
?>

The output of the code above will be:

Code: [Select]
Hello Peter!
Note: This will find every instance of the search string and replace them all.
You can also pass arrays as the search and replace variables instead.


These can be used for replacing profanity, creating a BB Code function, or used to create your own variable system. Such as adding a variable that users can use in-game, such as {USERNAME}. Then you just search for the string "{USERNAME}", and replace it with $username :)

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Replacing Strings!
« Reply #1 on: November 09, 2006, 10:50:52 PM »
str_ireplace() is new, and is not case sensitive...
and sometimes.. you have to use regex to do the job...

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal