How to get before after comma without using PHP explode() [duplicate] - php

This question already has answers here:
Remove portion of a string after a certain character
(15 answers)
Closed 2 years ago.
I know I can do this like following way, but is there a one line method like using regex to get everything before the comma as $first and everything after comma as $second?
$data = "m-12,s-52";
$arr = explode(",", $data);
$first = $arr[0];
$second = $arr[1];

You can get the $first and $second using this one line regex code
$data = "m-12,s-52,s-52";
$beforefirstcomma = preg_replace("/,(.+)/", "", $data);
$afterlastcomma = preg_replace("/(.+),/", "", $data);
$afterfirstcomma = preg_replace("/^[^,]+,+/", "", $data);
// outputs
echo $beforefirstcomma; // m-12
echo "<br>";
echo $afterlastcomma; // s-52
echo "<br>";
echo $afterfirstcomma; // s-52,s-52
Final Note
I still recommend using explode() because it is more efficient and performant than running preg_replace() multiple times on the $data string.

Related

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

How do i get the part of the url in php [duplicate]

This question already has answers here:
PHP get the last 3 elements of an associative array while preserving the keys?
(4 answers)
Closed 4 years ago.
How do I get Last 3 parts of the url
For example , I have link like below
http://www.yoursite/one/two/three/drink.pdf
I will get the last part of the url using below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
But i need last 3 parts from the url like below
/two/three/drink.pdf
Please provide me a solution.
You could do this:
<?php
$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);
$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf
First, you use explode to break the url string into an array. Then use array_slice to get the last three elements of the array and finally implode to glue back the array elements using / as the glue.
Putting it all in one line would look like:
echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));
Try below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));
Hope this helps.

How to assign series of numbers to a single array [duplicate]

This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 7 years ago.
I have a from input that contains series of numbers separate by white space that looks like this:
$input = "1243 984 4692 9465 ";
Is there any way to convert this to an array that keeps those numbers like this looks like this:
$array_numbers[0] = 1243
$array_numbers[1] = 984
$array_numbers[2] = 4692
$array_numbers[3] = 9465
Any help on this will be much appreciated,
Thank you
Try
$array_numbers = explode(' ', trim($input));
Trim and explode will do that for you.
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.trim.php
Trim removes the whitespace at the end, which if it stays will cause an empty entry in the array.
$input = "1243 984 4692 9465 ";
$array_numbers = explode(' ', trim($input));
The best way to do this is to use the explode function in php that separate the content of your string into an array of string, then convert each elements of array items into integer using intVal() function in php
See sample code below
<?php
$hello = "12 34 45 56 78";
$result = array();
$result = explode(" ", $hello);
for($j=0; $j<count($result); $j++){
echo intval( $result[$j] )."</br>";
}
?>

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