Limited Characters on a line [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 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.

Related

HTML tags replacing with 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 2 years ago.
Improve this question
If I have a paragraph:
echo "^b(This sentence becomes bold), and ^i(this becomes italic).\nThen this becomes ^up(uppercase).";
how to replace ^b, ^i, ^up, \n into a HTML tags ?
This sentence becomes bold, and this becomes italic.
Then this becomes UPPERCASE.
Thankyou.
You could try to use preg_replace() with patterns to do this:
<?php
// your example text
$text = "^b(This sentence becomes bold), and ^i(this becomes italic).\nThen this becomes ^up(uppercase).";
// array of patterns
$patterns = [];
$patterns[0] = "/\^b\((.*?)\)/";
$patterns[1] = "/\^i\((.*?)\)/";
$patterns[2] = "/\^up\((.*?)\)/";
// array of replacements
$replacements = [];
$replacements[0] = '<b>${1}</b>';
$replacements[1] = '<i>${1}</i>';
$replacements[2] = '<span style="text-transform:uppercase;">${1}</span>'; // or use something better here
// process the text
$formattedText = preg_replace($patterns, $replacements, $text);
// see the result
echo $formattedText;
?>
It would be much better if you write this logic in a helper function so you could use it easier on different places later.

How can i aa multi "-" with str_replace 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 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

How to substr before mark text? [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 want to cut text before "DETAIL" .
<?PHP
$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds "
//i want result Name:myproduct Price:99
?>
You are going to need to use substr which is used to get the substring of a string along with strpos which gives the position of the given occurance:
echo substr($text, 0, strpos($text, 'DETAIL'));
Visit php.net
You can split the text in array and get the first element of array:
$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds ";
$abc = explode('DETAIL',$text);
echo $abc[0];
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, 0, stripos($text, "detail"));
echo $clean;
This will output
Name:myproduct Price:99
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, stripos($text, "detail"), strlen($text) - 1);
echo $clean;
This will output
DETAIL :abcdedsfsdff 21348dfsdf ds

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

How to seperate a sentence like this [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 8 years ago.
Improve this question
How to separate the sentence by "comma" and at last using "and " in PHP Like
"Bruce_Campbell is an actor and director and creator and producer and musician."?
now i want to to show the the sentence like this...
""Bruce_Campbell is an actor , director ,creator , producer and musician.""
You don't want to replace the last and in the string.
$str = "Bruce_Campbell is an actor and director and creator and producer and musician.\n";
$except_last_and = substr_count($str, 'and') - 1;
$resStr = preg_replace("/and/", ' , ', $str, $except_last_and);
echo $resStr;
But Gautam3164 is correct you should Google first.
And the PHP manual is a great resource.
http://ca1.php.net/preg_replace
http://www.php.net/manual/en/function.substr-count.php
One line solution:
$str = "Bruce_Campbell is an actor and director and creator and producer and musician.";
print str_replace(" and ",", ", substr($str, 0, strripos($str, 'and'))).substr($str, strripos($str, 'and'), strlen($str));
Bruce_Campbell is an actor, director, creator, producer and musician.

Categories