I need to remove a single character from a string, eg in the text block below I would need to be able to remove ONE of the j's.
djriojnrwadoiaushd
leaving:
driojnrwadoiaushd
I need to remove a single character from a string, eg in the text block below I would need to be able to remove ONE of the j's.
djriojnrwadoiaushd
leaving:
driojnrwadoiaushd
Try this:
$needle = 'j';
if (($pos = strpos($str, $needle) !== false) {
$str = substr($str, 0, $pos) . substr($str, $pos+strlen($needle));
}
Just use substrings:
<?php
$str = 'djriojnrwadoiaushd';
$remove = 'j';
$index = strpos($str, $remove);
if ($index !== false)
$str = substr($str, 0, $index) . substr($str, $index + 1);
echo $str;
?>
Stand back - I know regular expressions;
$newString = preg_replace("/".preg_quote($stringToReplace)."/", '', $inputString, 1);
You can also use the fact that strings in PHP are arrays, and remove the element corresponding to the 'j':
$str[strpos($str, 'j')] = null;
You can also use str_relpace with the $count parameter: $str = 'djriojnrwadoiaushd'; echo str_replace('j', '', $str, 1);
Ups, sorry.. my bad.
Here is a real way:
$str = 'djriojnrwadoiaushd';
$pos = strpos( $str, 'j' );
if( $pos !== FALSE )
{
echo substr_replace( $str, '', $pos, 1 );
}