Jump to content

Recommended Posts

Hello,

here's a simple issue I'm having.  I have an array with 4 values as follows:

 

Array ( [0] => " KW " [1] => " KVAR " [2] => " " [3] => " " )

 

I want to strip the double quotes from each value, and count how many array cells actually contain an actual value once the quotes are removed. so I did this:

 

 


$used = 0;
for ($x = 0; $x < $4; $x++){
     $header_data[$x] = trim($header_data[$x], '"');
     if($header_data[$x] !== ""){
          $used++;
     }
}
print_r($header_data);
echo $used;


I thought this was very straight forward - apparently not! :S

 

here's my print_r of that array:

 

Array ( [0] => KW [1] => KVAR [2] => [3] => " ) 

 

and the output for my $used variable is "0"

 

First of all, for some reason, this will not remove the last double quote in the last array cell?? I'm so confused?  I don't get it.  And second, am I using the correct syntax for checking a null value in an array?

 

Thanks in advance for any suggestions, or help!  And if you have a more efficient way of performing this task, feel free to post! :P

CHEERS!

~C.

 

Link to comment
https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/
Share on other sites

here's my output from the var_dump()

 

array(4) { [0]=> string(7) " KW " [1]=> string(7) " KVAR " [2]=> string(7) " " [3]=> string(169) " " " }

 

obviously there's a third quote (which is really odd) - but the trim should remove every instance of the double quote, no?  What pray tel, would you suggest? :)

<?php
$header_data = array ( 0 => '" KW "', 1 => '" KVAR "', 2 => '" "', 3 => '" "' );
$used = 0;
for ($x = 0; $x < 4; $x++){
     $header_data[$x] = trim($header_data[$x], ' "');
     if($header_data[$x] != ""){
          $used++;
     }
}
print_r($header_data);
echo $used;

?>

Based on the lengths of the strings, you have a significant number of non-printing characters in the data (all elements). Either nulls, new-lines or something else...

 

Trim only trims from the start and end of the string. When it encounters a character not in the list of characters to be trimmed it stops. You probably want str_replace instead of trim and you may in fact want to remove all non-alphabetic characters.

Based on the lengths of the strings, you have a significant number of non-printing characters in the data (all elements). Either nulls, new-lines or something else...

 

Trim only trims from the start and end of the string. When it encounters a character not in the list of characters to be trimmed it stops. You probably want str_replace instead of trim and you may in fact want to remove all non-alphabetic characters.

Thanks for the suggestions. I'll work on those changes and let you know if I encounter any problems. Thanks for your help!

Okay - I've replaced the str_replace by inserting the following code:

$used = 0;
for ($x = 0; $x < $4; $x++){
     #new code line here#
     $header_data[$x] = str_replace('"',"",$header_data[$x]);
     #end new code#
     //$header_data[$x] = trim($header_data[$x], '"');
     if($header_data[$x] !== ""){
          $used++;
     }
}
print_r($header_data);
echo $used;

 

Doesn't really work out - my problem is that I can't figure out how to remove everything but letters in each of the array.

If anyone can help - I must say I'm still stuck. :(

<?php
$header_data[] = "testtest1234_'\"";
$header_data[] = "testtest5678-;:[].,<>?_'\"adsf";

$used = 0;

for ($x = 0; $x < 2; $x++){
     #new code line here#
     $header_data[$x] = preg_replace('~([^a-z]+)~i',"",$header_data[$x]);
     #end new code#
     //$header_data[$x] = trim($header_data[$x], '"');
     if($header_data[$x] !== ""){
          $used++;
     }
}
echo "<pre>";
var_dump($header_data);
echo $used;

die();
?>

 

 

Output:

array(2) {
  [0]=>
  string( "testtest"
  [1]=>
  string(12) "testtestadsf"
}
2

 

Thanks!

 

as soon as I tried the preg_replace() function - you posted...

 

I have one quick question though, is the '~([^a-z]+)~i' portion of the preg_replace() a specific structure?  I don't think I understand how entering those values will know to remove everything :-\

 

Not sure I'm explaining my question properly...  ???

The ~ just states everything in between is part of the regex. The ( ) is the conditional. Inside the [ ] is the characters we want to test. If the item is not a-z it gets replaced the ^ before the a-z indicates not. The + says match 1 (it may be 0) or more characters that are not a-z. If it finds the match then it replaces it.

 

Regex can be complicated and take some time to understand, but is very powerful and useful.

 

EDIT:

To any regex experts, if what I have stated is wrong, please correct me :) I am still learning regex.

The ~ just states everything in between is part of the regex. The ( ) is the conditional. Inside the [ ] is the characters we want to test. If the item is not a-z it gets replaced the ^ before the a-z indicates not. The + says match 1 (it may be 0) or more characters that are not a-z. If it finds the match then it replaces it.

 

Regex can be complicated and take some time to understand, but is very powerful and useful.

Awesome!  Thank you so much!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.