Convert space seperated string to class in php - php

I have a string 'abc back'
Is there a simple way to convert this to like this "AbcBack" in PHP

Just try with ucwords:
$input = 'abc back';
$output = str_replace(' ', '', ucwords($input));

Use ucfirst() function in php.Use the code below
<?php
$string='abc back';
$p = explode($string," ");
$text="";
foreach($p as $m){
$text .= ucfirst($m);
}
echo str_replace(" ","",$text);// will print AbcBack
You can use ucwords() to. Use the code below
<?php
$string='abc back';
echo str_replace(" ","",ucwords($string));
Hope this helps you

Related

Include php preg_replace in string

I am trying to include a php function within a string but my syntax is wrong. Could anyone help please?
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . $string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
echo strtolower($string) . "></span></div>";
Try this:
$string=get_field('br_category');
$string=preg_replace("/[^a-zA-Z]/", "", $string);
$string = "<div id=\"apostnav\"><span class=\"calcir b-" . strtolower($string) . "\"></span></div>";
I moved up two lines of code andn removed an echo from the last one.
Here the code with few optimizations:
$string = get_field('br_category');
$string = preg_replace("/[^a-zA-Z]/", '', $string);
$stringLower = strtolower($string);
$string = "<div id='apostnav'><span class='calcir b-$stringLower'></span></div>";
You can use printf too in order to print your html string with the variable:
printf('<div id="apostnav"><span class="calcir b-%s"></span></div>', strtolower(preg_replace('/[^a-zA-Z]/', '', get_field('br_category'))));

How to remove 4th letter in string using PHP?

How to remove 4th letter in string using PHP ?
I use this code.
<?php
$str = "1234567890";
$str2 = mb_substr($str, 4);
echo $str2;
?>
But it's will echo 567890
I want to echo 123567890 remove 4 from string.
How can i do ?
You can try substr_replace for this. Here we are replacing 4 which is at 3rd index.
Try this code snippet here
<?php
$str = "1234567890";
echo substr_replace($str, "", 3,1);
try setting the 3rd index to null
<?php
$str = "1234567890";
$str[3] = null;
echo $str;
try with below sulution:
$str = '1234567890';
$str_arr = str_split($str);
unset($str_arr[3]);
echo implode('', $str_arr);
output:
123567890
There are multiple ways of performing any operations on string variables in php
// can be used for printing purpose
$str = "1234567890";
echo substr($str,0,3).substr($str,4);
// actual replacement of string
$str = "1234567890";
echo substr_replace($str, "", 3,1);

How to reverse a string and replace chars?

I have been asked by an interviewer for a test:
$string = "This is sample string";
// Output: "string_sample_is_This";
what does this question mean..?
I assumed that he was asking about a simple echo, am I right or would he expected something else from me ?
<?php
$string = "This is sample string";
echo $string;
?>
He definetly asked for something else ...
<?php
$string = explode(" ","This is sample string");
$string = array_reverse($string);
$string = implode("_",$string);
echo $string;
?>
More :
PHP explode()
PHP array_reverse()
PHP implode()
That looks more like you are being asked to reverse the order of the tokens in the string and replace the delimiter from single spaces to the _ character. I would assume that or something similar to such was being asked.
No he told to reverse the sentence and echo the string.
<?php
$string = "This is sample string";
$string1 = '';
$string_array = explode(' ',$string);
$count = count($string_array) - 1;
for($i=$count; $i>=0; $i--)
{
if($i == 0)
{
$string1 .= $string_array[$i];
}
else
{
$string1 .= $string_array[$i]."_";
}
}
echo $string1;
?>

Replace a delimiter by html tag in php

I am trying to find a way to replace a string as the following:
Original string:
|Hello||everybody|, I am |human|
And result:
<span>Hello</span><span>everybody</span>, I am <span>human</span>
Is there a simple way to replace this original string to this result.
Thanks in advance.
preg_replace(
"~\|(.+)\|~U",
"<span>$1</span>",
$yourString
);
ideone demo.
I'm not really good at regexp, so here is an other code:
$string = "|Hello||everybody|, I am |human|";
$arr = explode("|", $string);
$result = "";
$span = "<span>";
$span_close = "</span>";
foreach($arr as $element){
if(strlen($element) > 0){
$result .= $span.$element.$span_close;
}
}
echo $result;

i want text and numeric part from the string in php

i have one string
$str ='california 94063';
now i want california and 94063 both in diferent variable.
string can be anything
Thanks in advance....
How about
$strings = explode(' ', $str);
Assuming that your string has ' ' as a separator.
Then, if you want to find the numeric entries of the $strings array, you can use is_numeric function.
Do like this
list($str1,$str2)=explode(' ',$str);
echo $str2;
If your string layout is always the same (say: follows a given format) then I'd use sscanf (http://www.php.net/manual/en/function.sscanf.php).
list($str, $number) = sscanf('california 94063, "%str %d");
<?php
$str ='california 94063';
$x = preg_match('(([a-zA-Z]*) ([0-9]*))',$str, $r);
echo 'String Part='. $r[1];
echo "<br />";
echo 'Number Part='.$r[2];
?>
If text pattern can be changed then I found this solution
Source ::
How to separate letters and digits from a string in php
<?php
$string="94063 california";
$chars = '';
$nums = '';
for ($index=0;$index<strlen($string);$index++) {
if(isNumber($string[$index]))
$nums .= $string[$index];
else
$chars .= $string[$index];
}
echo "Chars: -".trim($chars)."-<br>Nums: -".trim($nums)."-";
function isNumber($c) {
return preg_match('/[0-9]/', $c);
}
?>

Categories