This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 7 years ago.
I'm wondering how to extract numbers from a string for example
$string = "1.8 to 250"
What i want to get is,
$a = 1.8
$b = 250
Thanks for any help provided
Try this :
$str = '1.8 to 250';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
From this post Extract numbers from a string
Try this:
$str = '1.8 to 250';
$string = explode(" ",$str);
//print_r($string);
for($i = 0;$i < count($string);$i++){
if(is_numeric($string[$i])){
print "\n $string[$i]";
}
}
Related
This question already has answers here:
PHP - split String in Key/Value pairs
(5 answers)
Parse query string into an array
(12 answers)
Closed 1 year ago.
I have the following string:
$string = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
I'd like some PHP to grab the name and email address values from this string.
I'd originally tried the following but made no progress:
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
preg_match_all('#value=([^\s]+)#', $str, $matches);
echo implode(' ', $matches[1]);
I'd essentially like to say:
echo $string['No+Label+name']; // outputs name
Not a great e answer but you can use this if you have this string
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
$temp = explode('+', $str);
$name = explode('=',$temp[2]);
$email = explode('=',$temp[5]);
echo ($name[1]);
echo "<br/>";
echo ($email[1]);
This question already has answers here:
Most used words in text with php
(4 answers)
Closed 5 years ago.
I'm trying to find the most used word in a string in PHP. The lyrics file is a string of lyrics.
//display the most used word in the lyrics file
$wordCount = str_word_count($lyrics);
$wordCountArray = array();
foreach($wordCount as $word){
if(!array_key_exists($word, $wordCountArray)){
$wordCountArray[$word] = 1;
}else{
$wordCountArray[$word] += 1;
}
}
arsort($wordCountArray);
print_r($wordCountArray[0]);
I'm getting errors with this code and I'm wondering what isn't working. I need the actual word and not the number.
I think you meant:
$words = str_word_count($lyrics, 1)
foreach($words as $word) {
Simple example
<?php
$string = 'Hello hi hello abcd abc hoi bye hello';
$string = strtolower($string);
$words = explode(' ',$string);
var_dump(array_count_values($words));
This question already has an answer here:
How to replace multiple items from a text string in PHP? [duplicate]
(1 answer)
Closed 6 years ago.
I have the following string:
$string = java-developer-jobs
I would like to remove - and jobs from $string, but I don't want to use str_replace multiple times.
Use array as :
$toReplace = array('-','jobs');
$with = array('','');
$string = 'java-developer-jobs';
str_replace($toReplace,$with,$string);
You can use regex for this.Try something like this:
$string = "java-developer-jobs";
$sentence = preg_replace('/\-|jobs/', ' ', $string);
echo $sentence;
Output: java developer
LIVE DEMO
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
i have string like this:
<?php $string = "52.74837280745686,-51.61665272782557"; ?>
i want access to first string before comma and second string after comma like this:
<?php string1 = "52.74837280745686"; $string2 = "-51.61665272782557" ; ?>
thank you !
This is quite straigtforward
<?php
list($string1, $string2) = explode(",", $string);
or more excant
<?php
$splitTokens = explode(",", $string); // this is what "split" is in other languages, splits an spring by a SEP and retrieving an array
$string1 = $splitTokens[0];
$string2 = $splitTokens[1];
This question already has answers here:
How to strip trailing zeros in PHP
(15 answers)
Closed 7 years ago.
I have a string like this:
14522354265300000000000
I want to display it without zero values, how I can do this? I do this
$pos = strpos($route, '0');
$length = count(str_split($route));
$a = $length - $pos;
$a = substr($route, 0, $a);
but it remove 3 in the end of string. Can somebody help me?
Additional:
If string will be 123088888880, I want make it 123.
You can use rtrim for this:
echo rtrim("14522354265300000000000", "0"); // outputs: 145223542653
here's a nice algo:
<?php
$string = "14522354265300000000000";
$new_string = '';
for($i=0; $i<strlen($string) ; $i++){
if($string[$i] != '0'){
$new_string .= $string[$i];
}
}
echo $new_string;
?>
rtrim is only if you have zero's at end of string :)
You can use rtrim('14522354265300000000000', '0')