How can i aa multi "-" with str_replace in PHP [closed] - php

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 4 years ago.
Improve this question
my str is :
999999999
Now, how can i add "-" in this str ?
my example with "-" :
99-9-9-99999

Try this code -
<?php
$str = "999999999";
$output = substr($str,1,2)."-".substr($str,2,1)."-".substr($str,3,1)."-".substr($str,4,5);
echo "$output";
?>
output - 99-9-9-99999

You can use preg_replace() to format the number.
<?php
$num = "999999999";
$new_num = preg_replace("/([0-9]{2})([0-9]{1})([0-9]{1})([0-9]{5})/", "$1-$2-$3-$4", $num);
echo $new_num;
?>
Output:
99-9-9-99999

Related

How can I evaluate php code in a string variable? [closed]

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 1 year ago.
Improve this question
If I use the below code, I get the literal string <b> DATE: <?php echo $date; ?> </b> appended to content:
$content .= '<b> DATE: <?php echo $date; ?> </b>';
$pdf->writeHTML($content);
How can I instead get the value of $date there?
You are trying to put php code inside a string which will not be evaluated again. Try this:
$content .= '<b> DATE: ' . $date . '</b>';
$pdf->writeHTML($content);

Delete any string in front of first <br> appears, and any string after the last <br> shows [closed]

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 2 years ago.
Improve this question
Original:
Aaron Wind<br/>45 Civic Drive<br/>Greensborough VIC 3088<br/>Australia<br/>‎0450111222
Goal:
45 Civic Drive<br/>Greensborough VIC 3088<br/>Australia
I found it difficult to archive this by using PHP.
$s = 'Aaron Wind<br/>45 Civic Drive<br/>Greensborough VIC 3088<br/>Australia<br/>‎0450111222';
// explode your string by `<br/>`
$parts = explode('<br/>', $s);
// remove first and last items
$parts = array_slice($parts, 1, -1);
// implode by `<br/>`
echo implode('<br/>', $parts);
use this and let me know by accepting the answer if it works and using of-course the triangle hhh :-)
<?php
$string="Aaron Wind<br/>45 Civic Drive<br/>Greensborough VIC 3088<br/>Australia<br/>‎0450111222";
$start=stripos($string,"<br/>")+5;
$last=strrpos($string,"<br/>");
$newstring=substr($string,$start,$last);
echo "$newstring";
?>

Remove underscore from php echo output [closed]

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
How do i remove underscore from this echo?
if($validation->fails())
{
echo '<div class="error_message">', ($validation->errors()->first('first_name')), '</div>';
}
Thank you.
what madness is this?
if($validation->fails()) {
echo '<div class="error_message">' . str_replace("_"," ",($validation->errors()->first('first_name'))) . '</div>';
}
To replace a string char you have to use str_replace function.
Here is an example:
$var = "Hello_World";
$var = str_replace("_"," ",$var);
echo $var;
Output:
Hello World

Limited Characters on a line [closed]

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
I am designing a small application which shoots out status-like tweets from users at the five latest ones. To keep it a small application, I need for it to accept only so many characters on a line and the drop a line just below it. So for example:
Noah: The best thing about Stackoverflow
is that it is full of amazing programmers.
Something along the lines of something like that above. Can you help me with the code below :
echo "<div style='position:relative;top:-20px;padding-top:10px;padding-left:30px;padding-right:30px;'>";
echo "<p> $first_name: $body. </p>";
if (strlen($body <= 100)) {
echo "\n";
}
echo "</div>";
}
wordwrap will do that for you
string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )
Example:
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);
echo "$newtext\n";
A very
long
wooooooo
ooooord.

Is possible to replace an expression with a text in PHP? [closed]

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
For exemple, it's possible to replace
{'a1b2,left'}
with
<img src="a1b2" class="left"/>
in Php?
Yes...
$str = preg_replace('/\{\'(.*?),(.*?)\'\}/', '<img src="$1" class="$2"/>', $str);
yes you can.
//strip unwanted characters.
$string = preg_replace("/[{'}]/", "", $string);
//convert string to array
$string = explode(',', $string);
<img src="<?php echo $string[0]; ?>" class="<?php echo $string[1]; ?>"/>

Categories