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

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.

Related

extract two different date string between one special characters [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
I have some problem.
I have some data from my post on date range picker
there are
"10/04/2013 - 10/26/2013"
I want to get
date1 = "10/04/2013"
and
date2 = "10/26/2013"
for my between date query..
please help me
Thank you for your attention
You need to use explode()
$string="10/04/2013 - 10/26/2013"; //Your string
$exploded = explode('-', $string); //Explode using -
echo $exploded[0]; //echo string 1
echo $exploded[1]; //echo string 2
Note: You will have to use trim() if you want to get rid of the white space, else in the explode() first parameter, use the spaces before and after the - like explode(' - ', $string)
For mysql query the date format should be as "YYYY-mm-dd"
Your input comes as MM/dd/YYYY
You may need to convert that to mysql format before firing the query
In that case you can use strtotime()
date("Y-m-d",strtotime("10/04/2013"));
will give you 2013-10-04

Remove specific match from a string [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
I want to remove a particular match of characters from a string.
For example i have strings;
topics-p10-new-model-cars
topics-p20-new-model-cars
topics-p30-new-model-cars
topics-p40-new-model-cars
Then i need the results as,
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
That means i want to remove p10-,p20-,etc..
.Those are the page numbers. It may be any number..
How can i do this..? Thanks in advance
Try this:
$result = preg_replace('/\-p\d+/', '', $string);
Note: I'm assuming that the string format does not change (I mean this [topics-p10-new-model-cars]). If my assumption is right.
Then you can do this
if (textBox1.Text.Contains("-p10-"))
{
//topics-p10-new-model-cars
String[] splited = textBox1.Text.Split(new char[] {'-'});
String rString = String.Format("{0}-{1}-{2}-{3}",
splited[0],splited[2],splited[3],splited[4]);
MessageBox.Show(rString);
}
//OR This method
if (textBox1.Text.Contains("-p10-"))
{
String result = textBox1.Text.Replace("p10-", "");
MessageBox.Show(result);
}
With 'preg-replace'::
For, example:
<?
echo preg_replace('/p[0-9]+\-/', '', 'topics-p10-new-model-cars');
?>
Follow this link:
http://www.php.net/manual/en/function.preg-replace.php

Display exploded array [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
This is a very noob question. The previous developer has coded the lines as below
$a = array("30"=>"ok","40"=>"yes");
$b = "hi";
$c = $a."|".$b;
$d = explode("|",$c);
print_r($d[0]);
How can I display the array array("30"=>"ok","40"=>"yes")? print_r($d[0]); seems to print just array
This prints "array" instead of the actual array values is because this line:
$c = $a."|".$b;
What you're doing is saying:
$c = [array] + [string] + [string];
which will force array to be transformed into a string, which is just "array"
if you really want a | separated string of array indices, you could theoretically do this:
$c = implode("|",$a)."|".$b;
But the real best solution here would be to add something to the array before exploding the array:
$a['50'] = 'hi';
$d = explode("|", $c);

How to count the same words found in two different strings using 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
Say you have:
$text1="i can wait for all night";
$text2="we can sing for all day";
I need a function that will tell me 3 words found to be the same in both statements
i.e can, for, all.
Thanx
You can use explode to get the array of words:
$text1="i can wait for all night";
$text2="we can sing for all day";
$text1arr = explode(" ", $text1);
$text2arr = explode(" ", $text2);
Then, you can use array_intersect to get the common words:
$result = array_intersect($text1arr , $text2arr);
Finally, you can use count to get the number of common words:
$num_in_common = [count][3]($result);
$words1 = str_word_count($text1, 2);
$words2 = str_word_count($text2, 2);
$common_words = $result = array_intersect($words1, $words2);
$common_count = sizeof($common_words);
As a heads up, SO is not a coding service, and people won't be happy when answers are simply asked for, with seemingly no effort on the part of the asker. However, here's a little bit to get you started.
The array_intersect function, when given two or more arrays, gives you an array of all of the values shared between the inputs.
As for finding the position of the values, the array_search function gives a key for a value in the array.
If all arrays share the same key for a value in the array_intersect output array, then you know that that value is shared by all arrays, and in the same position.
How to implement all this is up to you.

PHP explode(), delimiter being returned? [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 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.

Categories