how to traverse a 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
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to traverse a string

Post by jasongr »

Hello

I have a string which contains characters of different encoding
I need to traverse it and to remove any character that is below 32 or above 127

I am trying the following code

Code: Select all

for ($i=0; $i<mb_strlen($file); $i++) {
	$ch = $file[$i];
	$value = ord($ch);
	if ($value < 32 || $value > 127) {						
		$file = mb_substr($file, 0, $i) . mb_substr ($file, ($i+1));
	}
}
For some reason, this code doesn't remove all the bad characters
The problem could be either in line:
$ch = $file[$i];
or in line
$file = mb_substr($file, 0, $i) . mb_substr ($file, ($i+1));

here is an example:
$file ="×
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

I found the bug
The bug has nothing to do with encoding, but with advancing the iteration index

here is a correct version of the code:

Code: Select all

for ($i=0; $i<mb_strlen($file); ) {
	$ch = $file[$i];
	$value = ord($ch);
	if ($value < 32 || $value > 127) {						
		$file = mb_substr($file, 0, $i) . mb_substr ($file, ($i+1));
	}
	else {
		$i++;
	}
}
TJ
Forum Newbie
Posts: 20
Joined: Thu Nov 03, 2005 10:22 pm
Location: Nottingham, UK

Post by TJ »

You could use a simple regular expression:

Code: Select all

$subject = "the quick BROWN FOX \t 012345 $ \r\nthis is the next line";
echo preg_replace('/[^\x20-\x7F]/','', $subject);
What this does is replace all characters that don't match the range 0x20 - 0x7F (32 - 127).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

TJ wrote: What this does is replace all characters that don't match the range 0x20 - 0x7F (32 - 127).
preg_replace is not mb safe unless /u modifier is used and input string is in UTF-8 encoding

here's more info: http://us2.php.net/manual/en/reference. ... .php#58409
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

below should work as well

Post by wtf »

Code: Select all

$bad = array("b", "a", "d");

$where = str_replace($bad, "", $where);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: below should work as well

Post by Chris Corbyn »

wtf wrote:

Code: Select all

$bad = array("b", "a", "d");

$where = str_replace($bad, "", $where);
?????
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

without the mb_ type functions!

Code: Select all

$str = ''; // string to strip...

$new = '';

for ( $i = 0; $i < strlen ( $str ); $i++ )
{
	$a = ord ( ( $b = substr ( $str, $i, 1 ) ) );

	if ( $a >= 128 )
	{
		$i++;
	}
	else if ( $a >= 32 )
	{
		$new .= $b;
	}
}

echo $new;
::EDIT::

made it not repeat code


yj
Post Reply