Home
Username:
Password:
PHP Tutorials

Data / Replacing Text Using Regular Expressions




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

After those last 2 heavy weight chapters on regular expression, I think you deserve a bit of a reward, so I have split of the ereg replace function and given it its own short movie at the end of this chapter. The ereg replace function works quite similarly to the str_replace function but is a lot more powerful. Let’s take a look at an example. Within our sentence string this time there, a number of different color words all the words for colors and let’s say we want to replace all the different colors with one single color, let’s say that's our favorite color and we want to change all the color words to that, we can use ereg replace to do that very simply. First argument this takes is the regular expression that we want to use so in this case you can put in all the colors and then the second argument is the string that we want ereg replace to work on. Now where as in str_replace which search for this string of characters literally ereg replace allows us to search for a regular expression pattern and replace all instances of that pattern with whatever string we want to replace it with. In this case, we’re going to replace it with the color blue, which is the second argument and the third argument is the string that we want the ereg replace function to act on. Let’s save that and we’ll go to the browser and have a look at what that does. As we can see, it’s replaced the colors orange and purple and so on and green with the word blue. I’ve included this rather nonsensical example here just as a little reminder that computers in general and programming in specific is never reasonable, we have to do all the reasoning and computers will just follow it very faithfully and logically no matter what kind of non sense its going to spew out at the end. However we can see that ereg replace is a very simple way of replacing regular expressions with a string of our choosing.

Tutorial Information

Course: PHP
Author: Joshua Mostafa
SKU: 33332
ISBN: 1889347787
Release Date: 2002-03-26
Duration: 9.5 hrs / 92 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available