Grab values from string [duplicate] - php

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]);

Related

remove particular words from $string in php [duplicate]

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

How To Split String Before After Comma? [duplicate]

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];

extracting numbers from string in php [duplicate]

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]";
}
}

How can i explode this string? [duplicate]

This question already has answers here:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);

How can i make all array values in variable [duplicate]

This question already has answers here:
How to represent a PHP array as a comma-delimited string? [duplicate]
(2 answers)
Closed 8 years ago.
i want to make all array values in one variable
Example:
i have a title
$title = "my name is medo";
$words = explode(' ', $title);
The result is a
$words['0'] = my
$words['1'] = name
$words['2'] = is
$words['3'] = medo
i want to make it like that
$allwords = "my,name,is,medo";
thanks all
You can use implode:
implode ( "," , $words );
You can also do this with string functions, which is much easier for your case.
$originalString = "my name is medo";
$finalString = str_replace(" ", ",", $originalString);

Categories