In php i would like to put a string like that :
*1\t1\tSomejehjdbsj\t7\t10\t5\t10\t0\t0\t0\t0\t0\t0\t--:--\t0\t0\t0\t0\t0\t00:00:00\t0\t1\t0\t0\t1f7ef741\t15:42\t99\t1026\t1\t--:--\tShowVault\t0\t1f7ef74187664f03876538511f30a5af\tSomejehjdbsj\t0\t0\t00000000000000000000000000000000\t\t00000000000000000000000000000000\t00000000000000000000000000000000\t0\t0\tNC-Series\t1\t12\tSCOPE\t16\t-1\t0\t0\t0\t0\t0\t0\t0\t0\t0\t\t0\t\t\n"]}*
But each \t split my string and its differents values.
And the \n is another "row" of that kind of stuff.
I need those values so i thinks i could put it in an array, But i don't know how to do that.
thanks!
Try something like this:
$testString = 'AAA\tBBB\nCCC\tDDD';
$result = explode('\\t', $testString);
foreach ($result as $key => $current) {
if (strpos($current, '\\n')) {
$result[$key] = explode('\\n', $current);
}
}
var_dump($result);
See it in action here: https://3v4l.org/92ttB
Related
I found this code in another post which I found quite helpful but for me it's only half the equation. In line with the following code, I need to take the string from a database, explode it into the 2d array, edit values in the array and implode it back ready for storage in the same format. So specifically backwards in the same order as the existing script.
The code from the other post >>
$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode(" \n ", $data);
$out = array();
$step = 0;
$last = count($data);
$last--;
foreach($data as $key=>$item){
foreach(explode(' ',$item) as $value){
$out[$key][$step++] = $value;
}
if ($key!=$last){
$out[$key][$step++] = ' '; // not inserting last "space"
}
}
print '<pre>';
print_r($out);
print '</pre>';
The quoted code inserts separate array elements which just have a space as value. One can wonder what benefit those bring.
Here are two functions you could use:
function explode2D($row_delim, $col_delim, $str) {
return array_map(function ($line) use ($col_delim) {
return explode($col_delim, $line);
}, explode($row_delim, $str));
}
function implode2D($row_delim, $col_delim, $arr) {
return implode($row_delim,
array_map(function ($row) use ($col_delim) {
return implode($col_delim, $row);
}, $arr));
}
They are each other's opposite, and work much like the standard explode and implode functions, except that you need to specify two delimiters: one to delimit the rows, and another for the columns.
Here is how you would use it:
$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$arr = explode2D(" \n ", " ", $data);
// manipulate data
// ...
$arr[0][2] = "scary";
$arr[2][2] = "balad";
// convert back
$str = implode2D(" \n ", " ", $arr);
See it run on repl.it.
If I have a piece of code that works like this:
$i = 0;
$names = explode(",", $userInput);
foreach($names as $name) {
$i++;
}
It works perfectly, provided the user has placed a comma after each name entered into the html textarea this comes from. But I want to make it more user friendly and change it so that each name can be entered on a new line and it'll count how many lines the user has entered to determine the number of names entered into the field. So I tried:
$i = 0;
$names = explode("\n", $userInput);
foreach($names as $name) {
$i++;
}
But this just gives me "1" as a result, regardless the number of new lines in the textarea. How do I make my explode count new lines instead of basing the count on something specifically entered into the text string?
EDIT Thanks to the people who answered, I don't believe there were any wrong answers as such, just one that suited my original code better than the others, and functioned. I ended up adopting this and modifying it so that numerous blank line returns did not result in artificially inflating the $userInput count. Here is what I am now using:
if(($userInput) != NULL) {
$i = 0;
$names = explode(PHP_EOL, trim($userInput));
foreach($names as $name) {
$i++;
}
}
It trims the empty space from the $userInput so that the remainder of the function is performed on only valid line content. :)
Don't make it complicated, you don't have to explode it into an array, just use this:
(Just count the new line character (PHP_EOL) in your string with substr_count())
echo substr_count($userInput, PHP_EOL);
Try using the PHP end of line constant PHP_EOL
$names = explode(PHP_EOL, $userInput);
A blank string as input:
var_dump(explode("\n", ''));
gives this as a result from a call to explode():
array(1) { [0]=> string(0) "" }
so you could use a ternary statement:
$names = $userInput == '' ? array() : explode("\n", $userInput);
Maybe you can change the explode function with preg_split to explode the user string with a regex
$users = preg_split('/[\n\r]+/', $original);
That's the idea, but I'm not on the computer so I can't test my code.
That regex would split the string if it founds one or more line breaks.
I have an issue that I am stuck in,
I have a table called wpg_original_word with field tags, which contains comma separated values and what I want to achieve is search for tags which are starting with the letter 'd'
I have written the query
SELECT DISTINCT tags FROM wpg_original_word WHERE tags LIKE 'd%' ;
It returns the whole string like 'Diabetes, Nutrition, Dietician' , but I want only the Diabetes, Dietician.
Any help?
You could split the string with PHP, use the following code
<?php
$string = 'Diabetes, Nutrition, Dietician';
$array = explode(',', $string);
$result = array();
foreach ($array as $part) {
if (substr(strtolower(trim($part)),0,1) == 'd') {
array_push($result, trim($part));
}
}
var_dump($result);
See it in action here.
http://codepad.org/SVk7FPx9
You can use in_array() to check for a string in your query and then print it.
In a comma delimited string, in php, as such: "1,2,3,4,4,4,5" is it possible to say:
if(!/*4 is in string bla*/){
// add it via the .=
}else{
// do something
}
In arrays you can do in_array(); but this isn't a set of arrays and I don't want to have to convert it to an array ....
Try exploding it into an array before searching:
$str = "1,2,3,4,4,4,5";
$exploded = explode(",", $str);
if(in_array($number, $exploded)){
echo 'In array!';
}
You can also replace numbers and modify the array before "sticking it back together" with implode:
$strAgain = implode(",", $exploded);
You could do this with regex:
$re = '/(^|,)' + preg_quote($your_number) + '(,|$)/';
if(preg_match($re, $your_string)) {
// ...
}
But that's not exactly the clearest of code; someone else (or even yourself, months later) who had to maintain the code would probably not appreciate having something that's hard to follow. Having it actually be an array would be clearer and more maintainable:
$values = explode(',', $your_string);
if(in_array((str)$number, $values)) {
// ...
}
If you need to turn the array into a string again, you can always use implode():
$new_string = implode(',', $values);
hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);