Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi guy can you help me i have problem sometimes variable.
<?php
$ns = "test";
echo $ns;
print test but I don't want this.
output
t:e:s:t
please help?
Use chunk_split()
chunk_split() function splits the function.
In our case, it splits by every 1 character.
And adds a new character :.
We do not need last : character so, we are using rtrim().
<?php
$ns = "test";
$n = chunk_split($ns, 1, ':');
echo rtrim($n, ":");
?>
Demo
Please try below code :
$ns = "test";
$array = str_split($ns);
echo implode(":", $array);
<?php $ns = "test";
echo substr(chunk_split($ns, 1, ':'), 0, -1);
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
can you please help me with finding right function for exchanging numbers in string? Numbers are separated with ":".
For example
"2:0" to "0:2"
"101:50" to "50:101"
Thank you.
There are many ways to do it, you can try the any of the ways here.
<?php
//using regex
$re = '/(\d+):(\d+)/i';
$str = '50:101';
$subst = '$2:$1';
$result = preg_replace($re, $subst, $str);
echo "The string $str after exchange is ".$result;
echo PHP_EOL;
// concatenating parts after explode
$parts = explode(':',$str);
echo "The string $str after exchange is $parts[1]:$parts[0]";
echo PHP_EOL;
//using explode, array_reverse and implode
$str = '50:101';
$result = implode(':', array_reverse(explode(':',$str)));
echo "The string $str after exchange is ".$result;
?>
DEMO: https://3v4l.org/OkY18
Simply explode() the string and then reform it.
$str = '100:200';
$bits = explode(':',$str);
echo $bits[1] . ':' . $bits[0];
RESULT
200:100
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hey i wanna know how i can extract this data and do a echo
Example of echo:
Item: D608
Time: 17011511
Same with the another line after ,
$strings = "D608-1-1-17011511-0,D832-1-1-17011511-0";
Thanks you!
If -1-1- and -0 will be always same then you can do it via str_replace
Working example
$strings = "D608-1-1-17011511-0,D832-1-1-17011511-0";
$strings = explode(',', $strings);
foreach ($strings as $item)
{
$item = str_replace( array('-1-1-', '-0'), array('-', ''), $item);
$item = explode('-', $item);
echo "Item: <b>".$item[0]."</b> ";
echo "Time: <b>".$item[1]."</b><br>";
}
Output:
Item: D608 Time: 17011511
Item: D832 Time: 17011511
If you know the length/content of the string, you can use explode().
$strings = "D608-1-1-17011511-0,D832-1-1-17011511-0";
$exploded_string = explode(',',$string);
foreach ($exploded_string as $string) {
echo explode('-',$string)[0].' '.explode('-',$string)[3];
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to last word from our Variable for example
$name = "Salman Khan";
And I want ans in only because i want tp remove n in my string
$words = explode(' ',$name); // Break words into array
$noofwords = count($words); // Find out how many
unset($words[$noofwords-1]); // remove the last one (-1 because of zero-index)
$newstring = implode(' ',$words); //put back together
$newstring = substr($name, 0, strlen($name)-1);
Do you mean remove the last letter?
echo substr($name, 0, -1);
Or the last word?
echo explode(' ', $name)[0];
echo rtrim($name,"n");
OR
echo substr($name,0,-1);
OR
echo substr($name,0,strlen($name)-1);
Output
salman kha
Try out this one.
$name = 'salman khan';
$length = strlen($name);
$char = $name[$length-1];
echo rtrim($name,$char);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In PHP given this string:
$string = '/sometext?123#abc/moretext';
How can I test for the existence of the pattern "?123#abc/" which will always be enclosed by "?" and "/" but have varying inner-text that may include any text and symbols? The text outside of the pattern will also be different. I need to do this:
if ($string includes pattern ?*/) {
//load the inner value into a variable
//then remove the entire patern including the leading "?" and trailing "/" and replace with a single "/"
}
How do I do this?
<?php
$string = '/sometext?123#abc/moretext';
$pattern = '/\\?(.*?)\\//';
if( $pieces = preg_split($pattern, $string, Null, PREG_SPLIT_DELIM_CAPTURE)) {
echo($pieces[1] . "\n");
unset($pieces[1]);
echo(implode("/", $pieces) . "\n");
}
?>
--output:--
~/php_programs$ php 1.php
123#abc
/sometext/moretext
Try this
$s = '/sometext?123#abc/moretext';
$matches = array();
$t = preg_match('#\?(.*?)\/#s', $s, $matches);
if($matches[1])
echo "match";
else
echo "not";
Output
match
Codepad
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have the following parameters for example:
max_image_width=100,max_image_height=200,image_proportion=1.75
I want to get an array with:
array('max_image_width'=>100,'max_image_height'=>200,'image_proportion'=175);
$str = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
$cfg = parse_ini_string(
str_replace(',', "\n", $str)
);
print_r($cfg);
5.4
$a=[];foreach(explode(',',$i)as$b){$a[explode('=',$b)[0]]=explode('=',$b)[1];}
$output = array();
parse_str(str_replace(',', '&', 'max_image_width=100,max_image_height=200,image_proportion=1.75'), $output);
E.g. by using preg_match_all.
<?php
$t = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
preg_match_all('!([^=]+)=([^,]+)!', $t, $m);
$x = array_combine($m[1], $m[2]);
var_export($x);
prints
array (
'max_image_width' => '100',
',max_image_height' => '200',
',image_proportion' => '1.75',
)
( though there are tons of other ways to do it without regular expressions ;-) )