i want text and numeric part from the string in php - php

i have one string
$str ='california 94063';
now i want california and 94063 both in diferent variable.
string can be anything
Thanks in advance....

How about
$strings = explode(' ', $str);
Assuming that your string has ' ' as a separator.
Then, if you want to find the numeric entries of the $strings array, you can use is_numeric function.

Do like this
list($str1,$str2)=explode(' ',$str);
echo $str2;

If your string layout is always the same (say: follows a given format) then I'd use sscanf (http://www.php.net/manual/en/function.sscanf.php).
list($str, $number) = sscanf('california 94063, "%str %d");

<?php
$str ='california 94063';
$x = preg_match('(([a-zA-Z]*) ([0-9]*))',$str, $r);
echo 'String Part='. $r[1];
echo "<br />";
echo 'Number Part='.$r[2];
?>
If text pattern can be changed then I found this solution
Source ::
How to separate letters and digits from a string in php
<?php
$string="94063 california";
$chars = '';
$nums = '';
for ($index=0;$index<strlen($string);$index++) {
if(isNumber($string[$index]))
$nums .= $string[$index];
else
$chars .= $string[$index];
}
echo "Chars: -".trim($chars)."-<br>Nums: -".trim($nums)."-";
function isNumber($c) {
return preg_match('/[0-9]/', $c);
}
?>

Related

How to multiply two numbers in a string

I have a string like "1X6TAB". Now I apply some regular expression to this string to remove "TAB" and replacing "X" with "*", so my final string will be "1*6". The expected result is "6", but I get "1*6" as the result.
Code:
Regexonlynumber("1X6TAB");
function Regexonlynumber($number){
$number = preg_replace("/[^0-9.X]/", '', $number);
echo str_replace('X',"*",$number);die;
}
Instead of str_replace(), you can explode() it by 'X' delimiter, then use array_product().
Regexonlynumber("1X6TAB");
function Regexonlynumber($number){
$number = preg_replace("/[^0-9.X]/", '', $number);
echo array_product(explode('X', $number));
}
You are almost there. Just add one step more to explode and multiply, like this:
<?php
function Regexonlynumber($number){
$number = preg_replace("/[^0-9.X]/", '', $number);
$arr = explode("X", $number);
echo $arr[0]*$arr[1];
}
Regexonlynumber("2X6TAB");
?>
Demo.
You can even just do it like:
function Regexonlynumber($number){
$arr = explode("X", $number);
echo $arr[0]*$arr[1];
}
Demo.
It's better to use eval(), which will also be able to compute more complex expressions:
Regexonlynumber("3X6-13TAB");
function Regexonlynumber($number){
$number = preg_replace("/[^0-9.X\-\+\/]/", '', $number);
$str = str_replace('X',"*",$number);
eval("\$result = {$str};");
echo $str . " = " . $result . "\n";
}
Regexonlynumber() function help you to to get product of numbers from string
array_product() Function :- The array_product() function calculates and returns the product of an array.
str_split() Function :- The str_split() function splits a string into an array.
<?php
function Regexonlynumber($number){
$number = preg_replace("/[^0-9,.]/", "", $number);
echo array_product(str_split($number));
}
Regexonlynumber("1X6TAB");
?>

How to remove 4th letter in string using PHP?

How to remove 4th letter in string using PHP ?
I use this code.
<?php
$str = "1234567890";
$str2 = mb_substr($str, 4);
echo $str2;
?>
But it's will echo 567890
I want to echo 123567890 remove 4 from string.
How can i do ?
You can try substr_replace for this. Here we are replacing 4 which is at 3rd index.
Try this code snippet here
<?php
$str = "1234567890";
echo substr_replace($str, "", 3,1);
try setting the 3rd index to null
<?php
$str = "1234567890";
$str[3] = null;
echo $str;
try with below sulution:
$str = '1234567890';
$str_arr = str_split($str);
unset($str_arr[3]);
echo implode('', $str_arr);
output:
123567890
There are multiple ways of performing any operations on string variables in php
// can be used for printing purpose
$str = "1234567890";
echo substr($str,0,3).substr($str,4);
// actual replacement of string
$str = "1234567890";
echo substr_replace($str, "", 3,1);

How to reverse a string and replace chars?

I have been asked by an interviewer for a test:
$string = "This is sample string";
// Output: "string_sample_is_This";
what does this question mean..?
I assumed that he was asking about a simple echo, am I right or would he expected something else from me ?
<?php
$string = "This is sample string";
echo $string;
?>
He definetly asked for something else ...
<?php
$string = explode(" ","This is sample string");
$string = array_reverse($string);
$string = implode("_",$string);
echo $string;
?>
More :
PHP explode()
PHP array_reverse()
PHP implode()
That looks more like you are being asked to reverse the order of the tokens in the string and replace the delimiter from single spaces to the _ character. I would assume that or something similar to such was being asked.
No he told to reverse the sentence and echo the string.
<?php
$string = "This is sample string";
$string1 = '';
$string_array = explode(' ',$string);
$count = count($string_array) - 1;
for($i=$count; $i>=0; $i--)
{
if($i == 0)
{
$string1 .= $string_array[$i];
}
else
{
$string1 .= $string_array[$i]."_";
}
}
echo $string1;
?>

php extract a sub-string before and after a character from a string

need to extract an info from a string which strats at 'type-' and ends at '-id'
IDlocationTagID-type-area-id-492
here is the string, so I need to extract values : area and 492 from the string :
After 'type-' and before '-id' and after 'id-'
You can use the preg_match:
For example:
preg_match("/type-(.\w+)-id-(.\d+)/", $input_line, $output_array);
To check, you may need the service:
http://www.phpliveregex.com/
P.S. If the function preg_match will be too heavy, there is an alternative solution:
$str = 'IDlocationTagID-type-area-id-492';
$itr = new ArrayIterator(explode('-', $str));
foreach($itr as $key => $value) {
if($value === 'type') {
$itr->next();
var_dump($itr->current());
}
if($value === 'id') {
$itr->next();
var_dump($itr->current());
}
}
This is what you want using two explode.
$str = 'IDlocationTagID-type-area-id-492';
echo explode("-id", explode("type-", $str)[1])[0]; //area
echo trim(explode("-id", explode("type-", $str)[1])[1], '-'); //492
Little Simple ways.
echo explode("type-", explode("-id-", $str)[0])[1]; // area
echo explode("-id-", $str)[1]; // 492
Using Regular Expression:
preg_match("/type-(.*)-id-(.*)/", $str, $output_array);
print_r($output_array);
echo $area = $output_array[1]; // area
echo $fnt = $output_array[2]; // 492
You can use explode to get the values:
$a = "IDlocationTagID-type-area-id-492";
$data = explode("-",$a);
echo "Area ".$data[2]." Id ".$data[4];
$matches = null;
$returnValue = preg_match('/type-(.*?)-id/', $yourString, $matches);
echo($matches[1]);

How to find out if there is any redundant word in string in php?

I need to find out if there are any redundant words in string or not .Is there any function that can provide me result in true/false.
Example:
$str = "Hey! How are you";
$result = redundant($str);
echo $result ; //result should be 0 or false
But for :
$str = "Hey! How are are you";
$result = redundant($str);
echo $result ; //result should be 1 or true
Thank you
You could use explode to generate an array containing all words in your string:
$array = explode(" ", $str);
Than you could prove if the arrays contains duplicates with the function provided in this answer:
https://stackoverflow.com/a/3145660/5420511
I think this is what you are trying to do, this splits on punctuation marks or whitespaces. The commented out lines can be used if you want the duplicated words:
$str = "Hey! How are are you?";
$output = redundant($str);
echo $output;
function redundant($string){
$words = preg_split('/[[:punct:]\s]+/', $string);
if(max(array_count_values($words)) > 1) {
return 1;
} else {
return 0;
}
//foreach(array_count_values($words) as $word => $count) {
// if($count > 1) {
// echo '"' . $word . '" is in the string more than once';
// }
//}
}
References:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.max.php
http://php.net/manual/en/function.preg-split.php
Regex Demo: https://regex101.com/r/iH0eA6/1

Categories