This question already has answers here:
PHP: Split string [duplicate]
(7 answers)
Closed 2 years ago.
I have values stored in a database that look like this
10_wc_asis
I need to isolate the values between the underscores (_) like this:
Value 1 = 10
Value 2 = wc
Value 3 = asis
How can I do that in PHP?
Try explode :
$str = "10_wc_asis";
$pieces = explode("_", $str);
var_dump($pieces)
Related
This question already has answers here:
How to mask parts of a string with the asterisk character
(5 answers)
Replacing last x amount of numbers
(6 answers)
How to replace string with asterisk(*) using strlen?
(6 answers)
Mask credit card number in PHP
(12 answers)
How can I hide/replace a part of a string in PHP? [duplicate]
(1 answer)
Closed 11 months ago.
I have the below string:
$string = 005390326548;
How can i get this result?
0053****6548
Basically i want to replace starting from the 4 characters the next 4 characters by ****
one-liner solution.
$string= substr_replace("8487013103", "****", 4, 4);
echo $string;
$string = '005390326548';
$retult = substr($string, 0, 4) . '****' . substr($string, 8);
This question already has answers here:
Get the last word of a string
(10 answers)
How to get the last element of an array without deleting it?
(33 answers)
Closed 1 year ago.
How can I remove all words from a string except the last one with PHP?
For example the string "Organic Black Olives" would become "Olives".
Try this:
$str = "Organic Black Olives";
// get words list
$words = explode(' ', $str);
// get last word
$last_word = $words[count($words)-1];
echo $last_word;
This question already has answers here:
Extract floating point numbers from a delimited string in PHP
(6 answers)
Closed 6 years ago.
I am trying to extract any number out from a string (with or without decimal) and dump that into an array. Below is my code
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/\d+/",$str,$matches);
echo var_dump($matches);
The current output has 3 elements below:
21
11
30
But I expect to output only 2 elements below:
21.11
30
How do I change my regex?
You should use this regex (add dot in character class) :
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/[\d\.]+/",$str,$matches);
var_dump($matches);
Test it (Ctrl + Enter)
This question already has answers here:
Search and replace multiple values with multiple/different values in PHP5?
(5 answers)
Closed 7 years ago.
I am trying to replace , with . and ' with `
I can do it like this
$bodyweight = str_replace("'", "`", $form->data['bodyweight']);
$bodyweight = str_replace(',', '.', $bodyweight);
Is the a way to do it in one str_replace?
Just read the documentation:
$bodyweight = str_replace(array("'", ','), array("`", '.'), $form->data['bodyweight']);
This question already has answers here:
Remove all non-numeric characters from a string; [^0-9] doesn't match as expected
(4 answers)
Closed 7 years ago.
I have some phone formatted variable like these variables:
$phone1 = "(123) 123-1234";
$phone2 = "+1234567892";
$phone3 = "123-434-3432
How can I convert these variables to plain number in php ?
the results should be like these variables:
$new_phone1 = "1231231234";
$new_phone2 = "1234567892";
$new_phone3 = "1234343432";
Use preg_replace which uses regex!
preg_replace("/[^0-9]/","",'123-434-3432');
Removes everything but numbers!