i have a form textearea field where a user enter a summary like:
il est poli comej hhh kkbkbkbkb
jbbbjbjblb ljlllbbblblb bnlbggkgkgkjg
lkjhhlhlhlhlhlh fin.
I would like to output this in two lines ending with '...' without printing all like
il est poli
comej hhh
kkbbkbkb jbbbjblb...
How to handle this in php ?
<?php
$summary = explode($textarea,' '); //split user input in words
echo $summary [0], ' ',$summary [1], ' ',$summary [2]; //print first 3 words
echo '<br>'; //newline
echo $summary [3], ' ',$summary [4]; //print 2 more words
echo '<br>'; //newline
echo $summary [5], ' ',$summary [6]; //print 2 more words
echo '...'; //dots
?>
This can be:
<?php
$summary = explode($textarea,' '); //split user input in words
echo $summary [0], ' ',$summary [1], ' ',$summary [2],'<br>',$summary [3], ' ',$summary [4],'<br>', $summary [5], ' ',$summary [6],'...';
?>
$str = 'asdf asdf sadf asdf asf asdf asdf asdf sfd';
$cut_length = 100;
if (strlen($str) > $cut_length)
$str = substr($str, 0, $cut_length) .'...';
$line_length = 50;
$str_words = explpode(' ', $str);
$len = 0;
$str = '';
foreach ($str_words as $word) {
$str .= $word . ' ';
$len += strlen($word);
if ($len >= $line_length) {
$str .= '<br/>';
$len = 0;
}
}
Related
This question already has answers here:
Php: Concatenate string into all array items
(6 answers)
Implode an array with ", " and add "and " before the last item
(16 answers)
Closed 8 months ago.
I want to add variables with each array item but I do not have clue what to do? Here is my code. Can anyone help me, please? Thanks
$str = "Ice Cream has";
$optional= "flavor";
$items = array("vanilla","chocolate","mango");
$itemsCount = count($items);
$sentence = '';
if ($itemsCount == 1) {
$sentence = $items[0] . '.';
} else {
$partial = array_slice($items, 0, $itemsCount-1);
$sentence = implode(', ', $partial).' '. $optional. ' and ' . $items[$itemsCount-1];
}
return $str.': '.$sentence.'.';
I want an output should be:
"Ice Cream has: vanilla flavor, chocolate flavor and mango flavor."
But I am getting output:
"Ice Cream has: vanilla, chocolate flavor and mango ."
You need to concatenate $optional to all elements of the array, not just the result of implode(). You can use array_map() to create a new array with $optional added to each element.
$str = "Ice Cream has";
$optional= "flavor";
$items = array("vanilla","chocolate","mango");
$items1 = array_map(function($x) use ($optional) { return "$x $optional"; }, $items);
$itemsCount = count($items1);
$sentence = '';
if ($itemsCount == 1) {
$sentence = $items[0] . '.';
} else {
$partial = array_slice($items1, 0, $itemsCount-1);
$sentence = implode(', ', $partial). ' and ' . $items1[$itemsCount-1];
}
return $str.': '.$sentence.'.';
$str = "Ice Cream has: ";
$optional= "flavor";
$items = array("vanilla","chocolate","mango");
$count = count($items);
$c=0;
if ($count == 1) {
$str .= $items[0] . ' '.$optional.'.';
} else {
foreach( $items as $item){
$c++;
if( $c == $count){
$str = $str.' and '.$item.' '.$optional.'.';
}else if($c == $count-1){
$str = $str.$item.' '.$optional.'';
}else{
$str = $str.$item.' '.$optional.', ';
}
}
}
return $str;
I am trying to write some PHP code that will separate words when the are two with "&" and a comma when they are three and the last two with "&"
Something like this
$string = "stack over flow";
Print on screen like this "stack, over & flow";
Hope you noticed the comma and the ampersand.
Then when they are two words
$string = "stack overflow";
print like this echo "stack & overflow";
Here is my code I have been trying, but I am not getting it right:
$string = '1,2';
$list = explode(',',$string);
foreach($list as $row) {
if($list = 2) {
echo ''.$row.' &';
}
}
This should take into account the possibilities of one or more words. If there is more than one word, just remove the last word (using array_pop()) and implode() with , the remaining words.
If there is only 1 word, the result is the same as the original string...
$string = "stack over";
$list = explode(" ", $string);
if ( count($list) > 1 ) {
$last = array_pop($list);
$result = implode(", ", $list) . " & {$last}";
}
else {
$result = $string;
}
To add anchor tags to each word...
$list = explode(" ", $string);
$aTag = '<a href="#">';
if ( count($list) > 1 ) {
$last = array_pop($list);
$result = $aTag.
implode("</a>, {$aTag}", $list) . "</a> & {$aTag}{$last}</a>";
}
else {
$result = $aTag.$string."</a>";
}
echo $result;
thanks Nigel Ren.. you code was really helpfully
but their a correction i made.Here
$string = "stack over flow";
$list = explode(" ", $string);
$aTag = '<a href="#">';
if ( count($list) > 1 ) {
$last = array_pop($list);
$result = $aTag.
implode('</a>, '.$aTag.'', $list) . "</a> & {$aTag}{$last}</a>";
}
else {
$result = $aTag.$string."</a>";
}
echo $result;
thanks
EXAMPLE:
input = 2
text = aa bb cc
Will become: aa cc
The input for position is $_POST['position']
i have
$words = explode(" ", $_POST['string']);
for ($i=0; $i<count($words); $i++){
echo $words[$i] . " ";
}
$to_remove = 2;
$text = "aa bb cc";
$words = explode(' ', $text);
if(isset($words[$to_remove -1])) unset($words[$to_remove -1]);
$text = implode(' ', $words);
Pulling out the big guns REGEX !!!
$string = 'aa bb cc dd';
$input = 2;
$regex = $input - 1;
echo preg_replace('#^((?:\S+\s+){'.$regex.'})(\S+\s*)#', '$1', $string);
Output: aa cc dd
Foreach loops tend to make it a little easier to understand (IMO). Looks cleaner too.
$pos = 2;
$sentence = explode(" ", $_POST['string']);
foreach ($sentence as $index => $word) {
if ($index != $pos - 1) {
$result .= $word . ' ';
}
}
echo $result;
This sounds like a homework question. I'll take a stab at it though:
Code:
<?php
$string = trim($_POST['string']);
$parts = explode(" ", string);
$newString = "";
$position = intval($_POST['position']);
for($a = 0; $a < count($parts); $a++) {
if($a != $position) { // or $a != ($position - 1) depending on if you passed in zero based position
$newString = $newString . $parts[$a] . " ";
}
}
$newString = trim($newString);
echo "Old String: " . $string . "<br />";
echo "New String: " . $newString;
?>
Output:
Old String: aa bb cc
New String: aa cc
$input = 2;
$words = explode(" ", $_POST['string']);
unset($words[$input-1]);
$words = implode(" ", $words);
echo $words;
I'm taking input from textarea form and put it in the long line.
Input is like this:
Country: France
City: Paris
Street: Champ
Then put it in the long line:
$line = $_POST['input'];
now, I need to replace spaces and new lines with <SP> and <BR>, so I do this:
$line = str_replace(" ","<SP>",$line);
$line = str_replace("\n","<BR>",$line);
but I get this:
Country:<SP>France <BR> <BR>City:<SP>Paris <BR> <BR>Street:<SP>Champ
Now, if insted of \n I tried this:
$line = str_replace("\r","<BR>",$line);
I get similar result. If I do them both i Get similar results which obviously has some spaces in it. So Then I do this:
$line = str_replace(" ","",$line);
but it stays the same.
My whole code is:
$line = str_replace(" ","<SP>",$line);
$line = str_replace("\n","<BR>",$line);
$line = str_replace(" ","",$line);
echo $line;
How to remove spaces in this case?
Edit:
I did bin2hex and found out that the space is actually carriage return \r.
So this is the solution:
$line = str_replace("\r","",$line);
Why did \r behave like space in this case?
the comments will probably suffice but I thought I'd share this snippet, which given your example I had a punt at what you are trying to achieve.
<?php
// example string data
$str = "key1 value1 key2 value2 key3 value3";
// clean string
$str = str_replace(" ", " ",$str);
$str = str_replace("\n", " ", $str);
$str = str_replace("\r", " ", $str);
$arr = explode(" ", $str);
$out = "<ul>";
$cnt = 1;
foreach($arr as &$val) {
if ($cnt++ % 2 == 0) {
continue;
} else {
$out = $out . "<li>" . $val . ": " . current($arr) . "</li>";
}
}
$out = $out . "</ul>";
echo $out;
// output source
// <ul><li>key1: value1</li><li>key2: value2</li><li>key3: value3</li></ul>
?>
Variable $name (string) gives something like (5 possible values):
"Elton John"
"Barak Obama"
"George Bush"
"Julia"
"Marry III Great"
Want to add <br /> after the first whitespace (" " between words).
So, it should give when echo:
"Elton<br/>John"
"Barak<br/>Obama"
"George<br/>Bush"
"Julia"
"Marry<br/>III Great"
1) <br /> should be added only if there is more than one word in a string.
2) Only after the first word.
3) Can be more than 3 words in variable.
$name = preg_replace('/([^ ]+) ([^ ]+)/', '\1 <br />\2', $name, 1);
Testing it out...
$names=array(" joe ",
"big billy bob",
" martha stewart ",
"pete ",
" jim",
"hi mom");
foreach ($names as $n)
echo "\n". preg_replace('/([^ ]+) ([^ ]+)/', '\1 <br />\2', $n, 1);
..gives...
joe
big <br />billy bob
martha <br />stewart
pete
jim
hi <br />mom
if (($pos = strpos($name, " ")) !== false) {
$name = substr($name, 0, $pos + 1) . "<br />" . substr($name, $pos +1);
}
This implements all your requirements:
$tmp = explode(' ', trim($name));
if (count($tmp) > 1)
$tmp[0] = $tmp[0] . '<br />';
$name = trim(implode($tmp, ' '));
if (count(explode(' ', trim($string))) > 1) {
str_replace(' ', ' <br />', $string);
}