Page 1 of 1
Remove character from string
Posted: Thu Apr 14, 2011 11:52 am
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...
Re: Remove character from string
Posted: Thu Apr 14, 2011 12:18 pm
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;
Re: Remove character from string
Posted: Thu Apr 14, 2011 1:00 pm
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?
Re: Remove character from string
Posted: Thu Apr 14, 2011 1:19 pm
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.
Re: Remove character from string
Posted: Thu Apr 14, 2011 1:43 pm
by blade_922
Is there a way around this?
Re: Remove character from string
Posted: Thu Apr 14, 2011 1:49 pm
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;
Re: Remove character from string
Posted: Thu Apr 14, 2011 4:32 pm
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
Re: Remove character from string
Posted: Thu Apr 14, 2011 4:58 pm
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?
Re: Remove character from string
Posted: Fri Apr 15, 2011 1:05 am
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?