How can i trim with array of string in php. If I have an dynamic array as follows :
$arr = array(' ','<?php','?>',"'",'"');
so how can i use this array in trim() to remove those string, I tried very hard in the code below :
$text = trim(trim(trim(trim(trim($text),'<?php'),'?>'),'"'),"'");
but i can not use this because array is dynamic, it may have more than 1000 values.
It takes a lot of time to turn into a loop even after trying it.So I can do anything as follows
$text = trim($text, array(' ','<?php','?>',"'",'"') );
It's possible to apply trim() function to an array. The question seems to be unclear but you can use array_map(). Unclear because there are other enough possible solutions to replace substrings. To just apply trim() function to an array, use the following code.
$array = array(); //Your array
$trimmed_array = array_map('trim', $array); //Your trimmed array is here
If you also want to fulfill the argument requirement in trim() you can apply a custom anonymous function to array_map() like this:
$array = array(); //Your array
$trimmed_array = array_map(function($item){
return trim($item, 'characters to be stripped');
}, $array); //Your trimmed array is here
Related
I have an array..let say:
$array = [$a,$b,$c,$d];
How I can remove [ and ]?
The expected result would be:
$a,$b,$c,$d
I used some array function e.g array_slice but it does not fill my requirement. Any ideas?
Note: I need to pass all array elements to function as argument.
e.g: function example($a,$b,$c)
it sounds like you're after a string representation of the array, try using join() or implode() like this:
<?php
$array = [$a,$b,$c,$d];
$str = join(",", $array); // OR $str = implode(",", $array);
echo $str;
EDIT
after reading your question a little more carefully, you're trying to pass the array into a function call, to do that you need to use call_user_func_array():
<?php
function function_name($p1, $p2, $p3, $p4){
//do something here
}
$array = [$a,$b,$c,$d];
call_user_func_array('function_name', $array);
I have the below line which takes a string, explodes it to an array by ',' and then trims any whitespace for each array item.
$essentialArray = array_map('trim', explode(',', $essential_skills));
However when $essential_skills string = "" $essentialArray will equal Array ( [0] => )
But I need it to equal Array() so that I can call empty() on on it.
That is normal and logical behavior of the function.
Look at it this way: what does the explode() call return? An array with a single element which is the empty string. The array_map() call does not change that at all.
So you might ask: why does explode('') result in an array with a single element which is the empty string? Well, why not? It takes the empty string and splits it at all comma characters in there, so exactly no times. That means the empty string stays unaltered. But it does not magically vanish!
explode return Array ( [0] => ), so you need to check your string before array_map
here is a one solution
$exploderesult = $essential_skills ? explode(',', $essential_skills) : array();
$essentialArray = array_map('trim', $exploderesult );
The easiest solution here will be just filter result array, like this:
$essentialArray = array_filter(array_map('trim', explode(',', $essential_skills)));
but proper way is:
if ($essential_skills === '') {
$essentialArray = [];
}
else {
$essentialArray = array_map('trim', explode(',', $essential_skills));
}
Due to bad DB design, there may be several values in a column # each row in a table. So I had to take in every string, check if commas exist (multiple values) & place each element into the end of an array.
Did try out functions like strpos, explode, array_push etc. With the folllowing code, how do i input ONLY the multiple elements into the end of an array, without creating another & placing that into an existing array?
$test = array();
$test = array ("testing");
$str = 'a,b,c,d';
$parts = explode(',', $str);
array_push ($test, $parts); //another array inserted into $test, which is not what I want
print_r($test);
Use array_merge.
$test = array_merge($test, $parts);
Example: http://3v4l.org/r7vaB
Let's say I have a string, like this:
$my_string = 'hello_world-my_name_is-holl';
My goal is to run this through a function, and end up with:
'hello_world-my_name_is'
I want to get rid of everything after the last instance of a hyphen. There can be more than two. My idea was trying something like this:
$arr = explode("-", $my_string);
$arr = array_pop($arr);
$new_name = implode('', $arr);
But that doesn't seem to work. What's a good short way of achieving what I'm looking for?
The reason why your code doesn't work is twofold:
first off, array_pop() modifies the passed array PLUS returns what it popped off. That means what you are doing here is re-assigning "holl" (a string) to your $arr variable and therefore it is no longer an array. That is why the following implode function fails. The second argument needs to be an array, not a string.
Secondly, you'll want to use the hyphen as a glue when putting the array back together into a string. So, the following should work.
$my_string = 'hello_world-my_name_is-holl';
$arr = explode("-", $my_string);
$arr2 = array_pop($arr);
$new_name = implode('-', $arr);
echo $new_name;
As you can see, I assigned the popped-off "holl" to a new variable (in case you need it), but then imploded the original, but modified array $arr.
My code reads a line from a file, splits the line into elements, and is supposed to put the elements in an array.
I used explode, but it does not put the elements into the array in sequential order.
Example: for input
line: 1000 3000 5000
This is what happens
$a=fgets($file); // $a= 1000 3000 5000
$arr= explode(" ",$a);
$u=$arr[3]; // $u=1000
$w=$arr[6]; // $w=3000
$x=$arr[10]; // $x=5000
This is the desired order:
$u=$arr[0]; // $u=1000
$w=$arr[1]; // $w=3000
$x=$arr[2]; // $x=5000
Why doesn't explode put data sequentially into the array?
It always puts them in sequentially. IF you are seeing this behavior then you must have extra in the document that are being exploded upon resluting in empty array elements. You will either need to pre-process the string or prune empty elements from the array.
Alternatively you could use preg_split with the PREG_SPLIT_NO_EMPTY setting.
Dome examples of that you are trying to do:
// using regex
$arr = preg_split("/ /", $a, PREG_SPLIT_NO_EMPTY);
// pruning the array
$arr = explode(" ", $a);
$arr = array_keys($a, '');
In your example, it's going to put a new array entry per every space.
To get what you're looking for, the returned string would have to be "$u=1000_$w=3000_$x=5000" (underscores represent spaces here)
An alternate way to do this would be to use array_filter to remove the empty elements i.e.
$arr = explode( ' ', $a ); // split string into array
$arr = array_filter( $arr ); // remove empty elements from array
$arr = array_values( $arr ); // strip old array keys giving sequential numbering, i.e. 0,1,2