5 Replies - 194 Views - Last Post: 13 July 2014 - 05:15 AM Rate Topic: -----

#1 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 732
  • Joined: 06-July 13

How do you use preg_replace?

Posted 12 July 2014 - 04:35 AM

How do you use preg_replace? I understand what it does, it can replace multiple things in a string instead of many str_replace functions, but how? I just can't make any sense of it. I can see the characters to replace in examples, but they appear to have other characters in them such as backslashes or hashtags - why? I've looked at the documentation here, but I can't make any sense of that either. All I understand is the following:

$output = preg_replace('something_to_search_for', $input);


Is This A Good Question/Topic? 0
  • +

Replies To: How do you use preg_replace?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How do you use preg_replace?

Posted 12 July 2014 - 04:38 AM

It uses Regular Expressions as the search pattern. You need to learn those if you want to use that function effectively.

A great place to start is regular-expressions.info. It has some great tutorials you can use.

The PHP manual also has a section on that you should look at: PCRE Pattern Syntax

This post has been edited by Atli: 12 July 2014 - 04:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: How do you use preg_replace?

Posted 12 July 2014 - 02:53 PM

Note that str_replace can work on arrays, so it's able to "replace multiple things in a string" too, just not the same way. You need to decide what's best for your situation.
Was This Post Helpful? 0
  • +
  • -

#4 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 732
  • Joined: 06-July 13

Re: How do you use preg_replace?

Posted 13 July 2014 - 04:45 AM

This is the kind of thing I have currently, which function do you think would be more beneficial?

function replace_characters($string) 
{
	//Lower the string
	$string = strtolower ($string);
	
	//Replace with text
	$string = str_replace('&', 'and', $string);
	$string = str_replace('&', 'and', $string);

	
	//Replace with dashes
	$string = str_replace(' ', '-', $string);
	$string = str_replace('.', '-', $string);
	$string = str_replace('"', '-', $string);
	$string = str_replace('_', '-', $string);
	$string = str_replace('/', '-', $string);
	$string = str_replace('\\', '-', $string);
	$string = str_replace(':', '-', $string);
	$string = str_replace('~', '-', $string);
	$string = str_replace('*', '-', $string);
	$string = str_replace('_', '-', $string);
	$string = str_replace('+', '-', $string);
	$string = str_replace('=', '-', $string);
	
	//Replace with nothing
	$string = str_replace('\'', '', $string);
	$string = str_replace('%', '', $string);
	$string = str_replace('^', '', $string);
	$string = str_replace('|', '', $string);
	$string = str_replace('#', '', $string);
	$string = str_replace('!', '', $string);
	$string = str_replace('(', '', $string);
	$string = str_replace(')', '', $string);
	$string = str_replace('@', '', $string);
	$string = str_replace(';', '', $string);
	$string = str_replace('$', '', $string);
	$string = str_replace('', '', $string);
	$string = str_replace('`', '', $string);
	$string = str_replace('?', '', $string);

	
	//Get rid of duplicate dashes then return the string
	$string = str_replace('--', '-', $string);
	$string = str_replace('--', '-', $string);
	$string = str_replace('--', '-', $string);
	$string = trim($string, "-");
	
	return $string;
	
}


Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: How do you use preg_replace?

Posted 13 July 2014 - 05:03 AM

I'll let others deal with preg_replace. You can improve your str_replace greatly by using arrays. For starters, look at this:
<?php
$replace = str_split(' ."_/\\:~*+=');
$with = "-";
$string = 'This is a_test\. It is only a: test.';
echo $string."<br>";
$string = str_replace($replace, $with, $string);
echo $string."<br>";


Do you see how you can expand this to the rest of your code above?
Was This Post Helpful? 0
  • +
  • -

#6 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 732
  • Joined: 06-July 13

Re: How do you use preg_replace?

Posted 13 July 2014 - 05:15 AM

It's certainly a lot easier, and it would help with the duplicate unnecessary calls. However the only issue here would be replacing two dashes at a time using the str_split - I suppose I could just declare the array in the first place for that though. Three calls of that would be a lot easier than all that code, and it would be easier to edit. Thanks, I'll use that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1