I have longtext column in mysql db and want to expldoe it into array. But explode returns me only 1 string.
$temp_array_links[] = $item->links; //array value: http://google.com/ http://test.com/ http://test1.com/
$temp_string = implode(" ", $temp_array_links); //convert array to string
$info_array_links = explode(" ", $test_string); //explode string
echo 'Your link: LINK'; //should be http://google.com/ insted of http://google.com/ http://test.com/ http://test1.com/
In the third line, you used the wrong variable name $test_string, you should have used $temp_string
your $test_string should be $temp_string try
$temp_array_links = array('http://google.com/', 'http://test.com/', 'http://test1.com/');
echo $temp_string = implode(" ", $temp_array_links); //convert array to string
$info_array_links = explode(" ", $temp_string); //explode string
print_r($info_array_links);
Also for fetching array you need to use index of an array not $info_array_links[$user_id--] try $info_array_links[0] // 1, 2,3
try to implode and explode with a separated something like ;
$temp_string = implode(";", $temp_array_links); //convert array to string
$info_array_links = explode(";", $temp_string); //explode string
Related
I have a string of this type:
string(11) "2=OK, 3=OK"
from a text file. But I would like to convert it into an array of keys this type :
array (
[2] => Ok
[3] => Ok
)
I was wondering how we could do that in PHP.
Note:- I normally use explode() and str_split() for the conversions string into array but in this case I don't know how to do it.
use explode(), foreach() along with trim()
<?php
$string = "2=OK, 3=OK" ;
$array = explode(',',$string);
$finalArray = array();
foreach($array as $arr){
$explodedString = explode('=',trim($arr));
$finalArray[$explodedString[0]] = $explodedString[1];
}
print_r($finalArray);
https://3v4l.org/ZsNY8
Explode the string by ',' symbol. You will get an array like ['2=OK', ' 3=OK']
Using foreach trim and explode each element by '=' symbol
You can use default file reading code and traverse it to achieve what you want,
$temp = [];
if ($fh = fopen('demo.txt', 'r')) {
while (!feof($fh)) {
$temp[] = fgets($fh);
}
fclose($fh);
}
array_walk($temp, function($item) use(&$r){ // & to change in address
$r = array_map('trim',explode(',', $item)); // `,` explode
array_walk($r, function(&$item1){
$item1 = explode("=",$item1); // `=` explode
});
});
$r = array_column($r,1,0);
print_r($r);
array_walk — Apply a user supplied function to every member of an array
array_map — Applies the callback to the elements of the given arrays
explode — Split a string by a string
Demo.
You can use preg_match_all along with array_combine, str_word_count
$string = "2=OK, 3=OK" ;
preg_match_all('!\d+!', $string, $matches);
$res = array_combine($matches[0], str_word_count($string, 1));
Output
echo '<pre>';
print_r($res);
Array
(
[2] => OK
[3] => OK
)
LIVE DEMO
$data['subject']= "Languages > English";
//This is how I get the string before the symbol '>'.
$subject = substr($data['subject'], 0, strpos($data['subject'], "> "));
But Now I need to get word after the '>' symbol. How do I alter the code above?
Or using explode :
$array = explode(' > ', $data['subject']);
echo $array[0]; // Languages
echo $array[1]; // English
https://php.net/substr
$subject = substr($data['subject'], strpos($data['subject'], "> "));
But you should have a look at explode : https://php.net/explode
$levels = explode(" > ", $data['subject']);
$subject = $levels[0];
$language = $levels[1];
If you want the data before and after the >, I would use an explode.
$data['subject'] = "Languages > English";
$data['subject'] = array_map('trim', explode('>', $data['subject'])); // Explode data and trim all spaces
echo $data['subject'][0].'<br />'; // Result: Languages
echo $data['subject'][1]; // Result: English
You can do this way,
your string is converted in an array
then you keep the last value of your array
$data['subject']= "Languages > English";
$subject = end(explode('>',$data['subject']));
I have this string that contains the following quote-contained, comma-separated values:
"field","anotherfield","yetanotherfield"
I need to populate an array with the content of these fields, without the quotes.
What I'm currently doing is:
$string = str_replace('"', NULL, $string);
and then
$array = explode(',', $string);
It works, but it breaks when there's a comma inside any field. How can I prevent this?
first, trim " from the start and end of string.
$string = trim('"field","anotherfield","yetanotherfield","other, another"', '"');
and after explode "," between values you need.
$array = explode('","', $string);
To parse entire CSV file into an array you could use str_getcsv function:
$array = array_map( 'str_getcsv', file( 'path-to-file/file.csv' ) );
This question already has answers here:
Add quotation marks to comma delimited string in PHP
(5 answers)
Closed 1 year ago.
I have a variable with string value of 'laptop,Bag' and I want it to look like ' "laptop","Bag" 'or "laptop","Bag". How could I do this one? Is there any php function that could get this job done? Any help please.
This would work. It first, explodes the string into an array. And then implodes it with speech marks & finishes up by adding the opening & closing speech mark.
$string = "laptop,bag";
$explode = explode(",", $string);
$implode = '"'.implode('","', $explode).'"';
echo $implode;
Output:
"laptop","bag"
That's what str_replace is for:
$result = '"'.str_replace(',', '","', $str).'"';
This would be very easy to do.
$string = 'laptop,bag';
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
That should turn 'laptop,bag' into "laptop","bag".
Wrapping that in a function would be as simple as this:
function changeString($string) {
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
return $newString;
}
I think you can explode your string as array and loop throw it creating your new string
function create_string($string)
{
$string_array = explode(",", $string);
$new_string = '';
foreach($string_array as $str)
{
$new_string .= '"'.$str.'",';
}
$new_string = substr($new_string,-1);
return $new_string;
}
Now you simply pass your string the function
$string = 'laptop,Bag';
echo create_string($string);
//output "laptop","Bag"
For your specific example, this code would do the trick:
<?php
$string = 'laptop,bag';
$new_string = ' "' . str_replace(',', '","', $string) . '" ';
// $new_string: "laptop","bag"
?>
That code would also work if you had more items in that list, as long as they are comma-separated.
Use preg_replace():
$input_lines="laptop,bag";
echo preg_replace("/(\w+)/", '"$1"', $input_lines);
Output:
'"laptop","Bag"'
I think you can perform that using explode in php converting that string in to an array.
$tags = "laptop,bag";
$tagsArray = explode(",", $tags);
echo $tagsArray[0]; // laptop
echo $tagsArray[1]; // bag
Reference
http://us2.php.net/manual/en/function.explode.php
related post take a look maybe could solve your problem.
How can I split a comma delimited string into an array in PHP?
I'd like to explode a string, but have the resulting array have specific strings as keys rather than integers:
ie. if I had a string "Joe Bloggs", Id' like to explode it so that I had an associative array like:
$arr['first_name'] = "Joe";
$arr['last_name'] = "Bloggs";
at the moment, I can do:
$str = "Joe Bloggs";
$arr['first_name'] = explode(" ", $str)[0];
$arr['last_name'] = explode(" ", $str)[1];
which is inefficient, because I have to call explode twice.
Or I can do:
$str = "Joe Bloggs";
$arr = explode(" ", $str);
$arr['first_name'] = $arr[0];
$arr['last_name'] = $arr[1];
but I wonder if there is any more direct method.
Many thanks.
I would use array_combine like so:
$fields = array ( 'first_name', 'last_name' );
$arr = array_combine ( $fields, explode ( " ", $str ) );
EDIT: I would also pick this over using list() since it allows you to add fields should you require without making the list() call unnecessarily long.
You can make use of list PHP Manual (Demo):
$str = "Joe Bloggs";
list($arr['first_name'], $arr['last_name']) = explode(" ", $str);
$arr then is:
Array
(
[last_name] => Bloggs
[first_name] => Joe
)
You cannot do explode(" ", $str)[0] in PHP <= 5.3.
However, you can do this:
list($arr['first_name'], $arr['last_name']) = explode(" ", $str);