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.

1

5 Answers 5

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']); 
Sign up to request clarification or add additional context in comments.

3 Comments

Works fine ofc, but its not what the OP asked for ...
Yes, but I think that it meant exactly
And also I updated answer and add realization of what the OP asked for
0
<?php $title = str_replace(array(" ", "&", "+", "-", "_", "$", "^", "="), '-', $row['title']); ?> 

Untested, but should work.

Edit/ Yes it does.

Comments

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

1 Comment

that is what i mention the link.
0
Try this.. <?php $row['title'] = preg_replace('/(\&|\+|\-|\_|\s|\$|\^|\=)/','-',$row['title']); ?> 

Comments

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?

2 Comments

What will be the return code?
I have just updated my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.