PHP explode(), delimiter being returned? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm not sure why but the explode function doesn't seem to be working for me.
I have a string which contains one or more sets of comma-seperated values. These sets are delimied by starting / ending square brackets.
After stripping off the ending "[" and "]", I thought it would be simple to then use the explode function to get the results seperated by "][". Instead, I get something weird.
$rawInserts = '[1,2,3,4,5][2,3,4,5,6][3,4,5,6,7]';
$the_inserts = substr($rawInserts,1,strlen($rawInserts)-2);
echo "$the_inserts \n"; //returns "1,2,3,4,5][2,3,4,5,6][3,4,5,6,7"
$inserts = explode($the_inserts , "][");
echo print_r($inserts)."\n"; // returns one item array containing "][";
why is it returning "]["? (FYI, I tried this exact example and it fails).
Thanks in advance.

array explode ( string $delimiter , string $string [, int $limit ] )
Delimiter first, string second.

Switch the parameters. It's delimiter first and string as second parameter:
$inserts = explode('][', $the_inserts);

it should be $inserts = explode("][",$the_inserts );

I myself cannot remember the parameters for every function, so just go to the php.net website and search for the explode function.

Related

PHP Return Array from function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts

Why does while(0) not execute the code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
<?php
$efFect=0;
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while ($str_position =strpos($string22,$find,$effect)){
echo $find.'the postion in '. $str_position.'<br>';
$efFect = $str_position + $find_len ;
}
?>
0 is a falsy value. You should make your check more reliable by checking if strpos actually returns false.
<?php
$effect=0; // not $efFect as variables names are case sensitive in PHP
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while (($str_position = strpos($string22, $find, $effect)) !== False){
echo $find.'the postion in '. $str_position.'<br>';
$effect = $str_position + $find_len ;
}
?>
From the documentation:
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
Someone mentioned that your code creates an infinite loop but this is not the case. strpos accepts an offset as the third parameter which you have correctly used. On each iteration the offset is incremented such that the new search will begin at the position where the last found result string ends, hence avoiding the infinite loop.

PHP regexping the string (extracting) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to get a substring (see examples, bold part) from the string. All strings begin with "input" followed by 2 underscores with some (1 to 7) random chars between. Thank you!
Examples:
input_7ax8_SOME_INFO
input_3f0max2_SOME_OTHER_INFO
input_k_ANOTHERINFO-any-chars-possible:0123456789
Using the detection of "non underscore" + "underscore" times 2 and fetching everything that comes after that you can get the result you ask.
The ?: is meant for not returning the result of the parts with underscores because the () are needed to combine it together.
$input = 'input_k_ANOTHERINFO-any-chars-possible:0123456789';
preg_match( '~^(?:[^_]+_){2}(.*)$~', $input, $match );
var_export($match);
You just need explode and its third param :
<?php
$input = 'input_7ax8_SOME_INFO';
$input = explode("_",$input,2); // Split 2 times
$input[2] = '<b>'.$input[2].'</b>'; // Make the rest of the string bold
$input = implode("_",$input); // re joining
echo $input;
?>

Get String between to string preg_match [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have this preg_match in php to get the url string between to string. but my problem is i can't get any data out of it
preg_match( "/\[(gmap)\](.*?)\[\/(gmap)\]/si", $content, $url)
i need to get the string inside of this string
[gmap]http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false[/gmap]
but i can't get a result. why?
$url[2]
i need to have
http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false
This works perfectly fine :
<?php
$url="";
$content = '[gmap]http://maps.google.com/maps/api/staticmap?zoom=15&size=325x125&maptype=roadmap&markers=color:purple|40.718217,-73.998284&sensor=false[/gmap]';
preg_match( "/\[(gmap)\](.*?)\[\/(gmap)\]/si", $content, $url);
print $url[2];
?>
Cheers!
It's possible that you got an issue because the .* also includes the [ character.
You could use this instead :
preg_match("/\[(gmap)\]([^\[]*)?\[\/(gmap)\]/si", $content, $url);

How to count numbers in a string with php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi i have an dynamic string like this 5,6,7,8,11,25 in my database. I want to count how much numbers are in the string. In my example there are 6. But I don't find a solution. Please help me.
MySQL lacks an explode function so you have to query the data and do the count in php using explode() and count().
count(explode(",", $string));
The explode() function breaks the string into an array element for each string separated by a comma, and then the count() function returns the number of elements in that array.
$numbers = explode(',', '5,6,7,8,11,25');
echo count($numbers);
try something like
your string
$str = '5,6,7,8,11,25';
then use explode to convert the string into an array
$str_array = explode(',', $str);
and to get the number of elements of the array
$size = count($str_array);
To count the numbers you will have to use something like:
count(array_filter(explode(',', $string), "is_numeric"))
But be careful, every string value that looks numeric will be counted.

Categories