This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 8 years ago.
$string = "comment-454";
I want to disply only 454? not comment-
i am saving in db comment-454 , now i want to crop comment-.. how can i
can someone provide me something, because i am newbie in php..
thanks in advance..
$string = str_replace("comment-", "", "comment-454");
should replace what you want.
you mentioned comment-454 came from your database. So ill assume it's in $result.
Now you can do:
$string = str_replace("comment-", "", $result);
echo $string;
Use explote() http://php.net/manual/de/function.explode.php
$teile = explode("-", $string);
echo $teile[1]; // 454
Related
This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);
This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this
This question already has answers here:
how to do a sum on a string in php?
(5 answers)
Closed 8 months ago.
I was asked this question in a PHP test,
Question: How to get the sum of values?
$str = "1,2,3,4,5,6,7";
My Solution was:
// Splitting numbers in array and adding them up
$str = "1,2,3,4,5,6,7";
$num_array = explode(',', $str);
$str = 0;
foreach($num_array as $num) {
$str+=$num;
}
echo $str;
I was told that my solution is BAD, is it BAD? Can anyone please tell why is it BAD? And any better/best solutions?
Thanks, in advance
Well, your solution is correct, but they might be expecting optimized or efficient or smallest code. May be like this:
$str = "1,2,3,4,5,6,7";
echo array_sum(explode(',', $str));
you should use array_sum(explode(",",$yourarray)) after exploding the string rather than looping array . this would be more efficient.
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);
This question already has an answer here:
How to decode something beginning with "\u" with PHP
(1 answer)
Closed 8 years ago.
I have one string:
"Hello\u00c2\u00a0World"
I would like convert in:
"Hello World"
I try :
str_replace("\u00c2\u00a0"," ","Hello\u00c2\u00a0World");
or
str_replace("\\u00c2\\u00a0"," ","Hello\u00c2\u00a0World");
but not work!
Resolve!
str_replace(chr(194).chr(160)," ","Hello\u00c2\u00a0World");
If you would like to remove \u.... like patterns then you can use this for example:
$string = preg_replace('/(\\\u....)+/',' ',$input);
You are most of the way there.
$stuff = "Hello\u00c2\u00a0World";
$newstuff = str_replace("\u00c2\u00a0"," ",$stuff);
you need to put the return from str_replace into a variable in order to do something with it later.
This should work
$str="Hello\u00c2\u00a0World";
echo str_replace("\u00c2\u00a0"," ",$str);
You may try this, taken from this answer
function replaceUnicode($str) {
return preg_replace_callback("/\\\\u00([0-9a-f]{2})/", function($m){ return chr(hexdec($m[1])); }, $str);
}
echo replaceUnicode("Hello\u00c2\u00a0World");