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
Related
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.
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 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.
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]; ?>"/>
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 long a txt file list like this
dddd
ggggb
btbtgbgb
gtrbr
and I want each item to be like this:
'dddd'
'ggggb'
'btbtgbgb'
'gtrbr'
echo "'" . str_replace(' ', "' '", 'dddd ggggb btbtgbgb gtrbr') . "'";
// returns 'dddd' 'ggggb' 'btbtgbgb' 'gtrbr'
$str = "dddd ggggb btbtgbgb gtrbr";
echo "'" . str_replace(' ', "' '", preg_replace('/\s\s+/', ' ', $str)) . "'";
//returns 'dddd' 'ggggb' 'btbtgbgb' 'gtrbr' and strips extra whitespace if ever your text file has extra whitespaces.
// load file into an array
$lines = file($textfile);
foreach ($lines as $key => $line) {
// add quotes and remove tailing newline
$lines[$key] = "'".rtrim($line, "\n")."'";
}
print_r($lines);
Note: This will probably not scale very well.