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.
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 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";
?>
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
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
Fetch Bill no from a string (output from tesseract OCR)
Tesseract OCR string is as follows
1;FTC013233
259139 Bill Date 23/06/2015
Mrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e
Bill No 34939
Hospital ' Req No HG-4 1142645
3;HASH'KA'-A- D 9 %
Eergncy
VH)
a.. . !‘:‘u"‘_‘i"
Total Amount:
Paid Amount :
You can use preg_match along with Positive Lookbehind regex as
preg_match('/(?<=Bill\sNo\s)(\d+)\b/',$str,$res);
echo $res[0];//34939
You can use regex
/Bill\s+No\s+(\d+)/ig
Test here
PHP CODE :
<?php
$re = "/Bill\\s+No\\s+(\\d+)/i";
$str = "1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34939\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :1;FTC013233\n\n259139 Bill Date 23/06/2015\nMrs. DR.Greesshma-‘H Age/sex 23;y 22;D 1 Fema|e\n\nBill No 34930999\nHospital ' Req No HG-4 1142645\n\n3;HASH'KA'-A- D 9 %\n\n Eergncy\n\nVH)\n\na.. . !‘:‘u\"‘_‘i\"\n\nTotal Amount:\n\nPaid Amount :";
preg_match_all($re, $str, $matches);
print_r($matches);
?>
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
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.