Remove character from string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Remove character from string

Post by blade_922 »

Hi,

Having a bit of trouble. $boxart - holds a url with the name of a game, contains punctuation such as : and ! which i need to remove. I have tried below with no luck.

Code: Select all


$boxart="images/uploads/ds/$id-$name-$console.jpg";

$boxart = array(":"); 

 $new_box= str_replace($boxart, ' ', strtolower($newbox)); 

echo $new_box;

Nothing is printed out with this...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Remove character from string

Post by social_experiment »

Code: Select all

<?php

$boxart="images/uploads/ds/$id-$name-$console.jpg";

$boxart = array(":"); 

 $new_box= str_replace($boxart, ' ', strtolower($newbox)); 

echo $new_box;

?>
Your first variable should probably be called '$newbox'. Below is an example (based on your code) that uses arrays. If you have multiple values to be replaced you should use arrays with str_replace().
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Code: Select all

<?php
 $find_array = array(':', '!');
 $replace_array = array('a', 'b');

 $boxart = "images/uploads/ds/$id-$name-$console.jpg";
 $lower_boxart = strtolower($boxart);
 
 $new_box = str_replace($find_array, $replace_array, $lower_boxart);
 echo $new_box;
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Remove character from string

Post by blade_922 »

Hi,

Understood. It did what it was supposed to. BUT not what im trying to achieve. What it is, i have around 5000 images that are named like the following $id-$name-$category.jpg

Now im trying to insert the url of these images into the according row on the mysql database. The $id $name and $category is the same on both the db and the image name. So what i thought was to make up the url in the php script then somehow insert it into the database. But some images contain characters such as : colons and ! exclamation marks, so when i try to display the image it does not show.

What would be my best mothod for this?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Remove character from string

Post by social_experiment »

blade_922 wrote:But some images contain characters such as : colons and ! exclamation marks, so when i try to display the image it does not show.
Those characters are illegal in filenames, this could be your problem.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Remove character from string

Post by blade_922 »

Is there a way around this?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Remove character from string

Post by danwguy »

For starters your url string is wrong, it should be this...

Code: Select all

$boxart="images/uploads/ds/".$id."-".$name."-".$console.".jpg";
Then use the str_rplace() function with the proper arrays ie..

Code: Select all

$replace = array(":", "!");
$newbox = srt_replace($replace, '', strtolower($boxart));
echo $newbox;
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Remove character from string

Post by blade_922 »

Hi danwguy,

I get this error when i use what you put.

Call to undefined function srt_replace() in /home/public_html/videogamingcave/aaa/img/gameslist.php on line 33
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Re: Remove character from string

Post by blade_922 »

hhhmmm, STRTOLOWER converts everything to lower case. What if i dont want this to happen? How can i stop this form happening when i remove the characters?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Remove character from string

Post by social_experiment »

blade_922 wrote:Call to undefined function srt_replace() in /home/public_html/videogamingcave/aaa/img/gameslist.php on line 33
That srt_replace should be str_replace.
blade_922 wrote:hhhmmm, STRTOLOWER converts everything to lower case. What if i dont want this to happen? How can i stop this form happening when i remove the characters?
You could remove the strtolower() function?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply