How can i explode this string? [duplicate] - php

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

Related

How do I remove string after or before space in PHP? [duplicate]

This question already has answers here:
Return the portion of a string before the first occurrence of a character in PHP [duplicate]
(6 answers)
Closed last month.
How do I remove string after or before space in PHP?
I have a string like $string = "Hello World";
The output should be "Hello"
How can I do that?
I have a string like $string = "Hello World";
The output should be "Hello"
Well, you can explode on <space> and take the first element of the resulting array:
<?php
$a = "Hello World";
$arr = explode(" ",$a);
echo $arr[0]; // Hello
I can't help but think there's more to it than this.

Grab values from string [duplicate]

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

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

How to split a string into two variables using PHP? [duplicate]

This question already has answers here:
how to split a comma seperated string into a two variables using php [closed]
(10 answers)
Closed 8 years ago.
Using PHP how can I split the
Szombathely, Hungary
into two variables
Szombathely
Hungary
Thank you!
array explode ( string $delimiter , string $string [, int $limit ] )
Source: http://php.net/manual/en/function.explode.php
$split = explode(',','Szombathely, Hungary');
That will, however, leave you with space before _Hungary
So if all are ,_ you could use
$split = explode(', ','Szombathely, Hungary');
or use trim on each of array elements.
There is a native command called explode, in which you pass as parameters the delimiter and also the string. More info here
In your case it would be:
$result = explode ( ',', 'Szombathely, Hungary')
as a result you get:
$result[0] = 'Szombathely'
$result[1] = 'Hungary'
Hope it helps!
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;
var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary`

Categories