0

I want to remove the symbol from the title. When any user post their ads in my site they write anything with some symbol like "&, +, -, _, $, ^, =,". This type of symbol i want to remove automatic from the title. I have tried for the space and success. I used for removing space with "-" this code

  <?php 
  $title = str_replace(' ', '-', $row['title']) 
  ?>

I want to all this "&, +, -, _, $, ^, =," symbol. Please help me.

user3259990
  • 71
  • 1
  • 1
  • 5
  • You're probably looking for this: http://stackoverflow.com/questions/7605480/str-replace-for-multiple-items – Gohn67 Feb 04 '14 at 09:45

5 Answers5

2

Better use htmlentities PHP function for convert all applicable characters to HTML entities:

$title = htmlentities($row['title']);

or use it if you really have a string "&, +, -, _, $, ^, =" of symbols:

$symbols = explode(",", "&, +, -, _, $, ^, =");
$title = str_replace($symbols, "", $row['title']);
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
0
<?php 
    $title = str_replace(array(" ", "&", "+", "-", "_", "$", "^", "="), '-', $row['title']);
?>

Untested, but should work.

Edit/ Yes it does.

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
0

You can do something like this:

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

check here:Remove all special characters from a string

Community
  • 1
  • 1
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

How's about this?

$removeme = array("=", "+", "-", "_", "$", "^", "&", " ");
$finaltitle = str_replace($removeme , "-", $title);

EDIT

So if you assigned your title to $title and then ran it through str_replace as mentioned previously, it would possibly look like the below code:

$title = 'thi=s &is my-ti&tle_and^stuff';
$removeme = array("=", "+", "-", "_", "$", "^", "&", " ");
$finaltitle = str_replace($removeme , "-", $title);
echo $finaltitle // echos 'thi-s--is-my-ti-tle-and-stuff';

If you're just trying to generate a slug-url, could I recommend reading this link?

Paul Macey
  • 113
  • 9
0
Try this..
<?php
$row['title'] = preg_replace('/(\&|\+|\-|\_|\s|\$|\^|\=)/','-',$row['title']);
?>
Vegeta
  • 1,319
  • 7
  • 17