Author Topic: [SOLVED] simple string func not working correctly  (Read 361 times)

0 Members and 1 Guest are viewing this topic.

Offline scottybwoyTopic starter

  • Devotee
  • Posts: 530
  • Gender: Male
    • View Profile
[SOLVED] simple string func not working correctly
« on: November 20, 2008, 11:24:45 AM »
Hi I want to check if a specific character is at the begginign and end of some string data, then if it exists, chop it off.  This is what I have, but it doesn't work.  P.S. is there a simpler way to do it :
Code: [Select]
$f_char = substr($products_description, 0, 1);
$l_char = substr($products_description, -1);

if (($f_char == '"') && ($l_char == '"')) {
echo "cleaning description";
$len = strlen($products_description);
$products_description = substr($products_description, 1, $len -1);
}
« Last Edit: November 20, 2008, 11:28:18 AM by scottybwoy »

Offline genericnumber1

  • Addict
  • Posts: 1,854
    • View Profile
Re: simple string func not working correctly
« Reply #1 on: November 20, 2008, 11:35:08 AM »
You can actually use the bracket operators on a string, so I'd do it like this...

Code: [Select]
$first = $str[0]; // First char
$last = $str[strlen($str)-1]; // Last char
if($first == '"' && $last == '"') {
   $str = substr($str, 1, -1);
}

The reason yours didn't work is because you have to remember the third param of substr() is the number of characters to get... so if you go strlen() - 1 and you started at index 1 (second char), you'll go to the end of the string. You'd need to do strlen()-2 to chop an extra char off of the end.

Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,671
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: simple string func not working correctly
« Reply #2 on: November 20, 2008, 11:37:31 AM »
Code: [Select]
<?php
$products_description 
"\" TESTING \"";
$f_char substr($products_description01);
$l_char substr($products_description, -1);

echo 
$f_char " - f_char<br />" $l_char " - l_char<br />";

if ((
$f_char == '"') && ($l_char == '"')) {
   echo 
"cleaning description";
   
$len strlen($products_description);
   
$products_description substr($products_description1$len -2);
}
echo 
$products_description "<Br /><Br />";
?>


Simple debugging would of found that you needed to do $len - 2 since the substr is 0 index based.

Offline sasa

  • Guru
  • Fanatic
  • *
  • Posts: 3,011
  • Gender: Male
    • View Profile
Re: simple string func not working correctly
« Reply #3 on: November 20, 2008, 12:39:41 PM »
try
Code: [Select]
<?php
$products_description 
"\" TESTING \"";
echo 
trim($products_description'"');
?>

Offline genericnumber1

  • Addict
  • Posts: 1,854
    • View Profile
Re: simple string func not working correctly
« Reply #4 on: November 20, 2008, 01:17:01 PM »
Do note that using trim() will remove more than one consecutive quote from the edge of the string, and will still remove a quote from the beginning of the string if there is not a matching one at the end of the string, and vice versa. This is a bit different than your current implementation, but if you don't mind these differences trim() would be more efficient.
« Last Edit: November 20, 2008, 01:17:39 PM by genericnumber1 »

Offline scottybwoyTopic starter

  • Devotee
  • Posts: 530
  • Gender: Male
    • View Profile
Re: simple string func not working correctly
« Reply #5 on: November 21, 2008, 05:03:26 AM »
Thanks for your help.  Yeah trim() isn't quite what I was looking for.  I had done debugging first which is why I couldn't find the error.  My problem was that I was using someone else's script and it was working on the wrong data. lol.  Is sorted now, thanks