Laravel get column length and turn into spaces [duplicate] - php

I want to like add space in sting with specified string length.
For example : Hello is my string & i set string length 30 so next string should be start after length 30 so it should be like Hello Next string.
I would like to add space..
I have used following code but it's not adding space it's take whole character but not added space.
<?php
$fistString ="Hello World";
$secondString ="Good";
echo str_pad($fistString, 45, ' ', STR_PAD_RIGHT).$secondString;
?>

it will be done by using this
$string_1 = "The quick brown fox jumped over the lazy dog.";
$string_2= " -space-or-anything- ";
$newtext = wordwrap($string_1, 20, " $string_2 <br>");
// it will add second string after every 20th character and also break the line
echo $newtext;
// result will be like
The quick brown fox -space-or-anything-
jumped over the lazy -space-or-anything-
dog.

as per my knowledge the answer is :-
<?php
$fistString ="Hello World";
$secondString ="Good";
$str=$fistString;
for ($i=0; $i < 30; $i++) {
$str.=' ';
}
$str.=$secondString;
echo $str;
?>

I did with my self with following way..
<?php
echo str_pad($string, 30, " &nbsp", STR_PAD_RIGHT).$secondString;
?>

Add space with specified string length in php
$temp ="optionA"
chunk_split($temp ,6,' ')
output : option A

Related

php str_replace function issue

I am trying to have this ORIGINAL string converted to the RESULT below using php.
ORIGINAL: "The quick <font color="brown">brown</font> fox jumps over the lazy dog"
RESULT:"god yzal eht revo spmuj xof <font color="brown">nworb</font> kciuq ehT"
What I have done so far is explained like below.
First, strip the HTML tag from the ORIGINAL.
$originalStr = "The quick <font color='brown'>brown</font> fox jumps over the lazy dog";
$stripTags = strip_tags($originalStr);
This results to The quick brown fox jumps over the lazy dog ,
Second, I reverse the result and the word "brown" by using strrev function
$reverseStr = strrev($stripTags);
$brown = strrev("brown");
This results to god yzal eht revo spmuj xof nworb kciuq ehT
Third, I am trying to use str_replace function to find $brown from the reverseStr, and replace it with $openFont $brown $closeFont like below.
$openFont = "<font color='brown'>";
$closeFont = "</font>";
$result = str_replace($brown, $openFont.$brown.$closeFont, $reverseStr);
echo "result -->" . $result . "<br/><br/><br/>";
This results to god yzal eht revo spmuj xof kciuq ehT, NOT the same as the RESULT.
It seems like special characters in font () tag is the problem that may be blocking str_replace to replace the String.
$result = str_replace($brown, "TEST", $reverseStr);
echo "result -->" . $result . "<br/><br/><br/>";
This results to god yzal eht revo spmuj xof TEST kciuq ehT
Does anyone know str_replace is not accepting special characters? and know how I should solve this problem?
If there is another way to solve the problem, I will also be appreciated to hear your suggestion.
(* This is one of the practical questions that I am trying to solve in an algorithm test websites)
UPDATED: I feel so dumb to think that where font tag was. Since tag is meant to change the font color, it was working perfectly in the beginning. Thank you very much everyone for your time!
If it was me, I'd do this (fully tested):
// Original string
$str = 'The quick <font color="brown">brown</font> fox jumps over the lazy dog';
// Strip the font tag
$str = strip_tags( $str );
// Convert string to array
$arr = str_split( $str );
// Reverse the array
$rra = array_reverse( $arr );
// Convert array back to string
$str = implode( $rra );
// Add font tag back in
$str = str_replace('nworb', '<font color="brown">nworb</font>', $str);
// Result
echo $str;
Parse the HTML with something that will give you a DOM API to it.
Write a function that loops over the child nodes of an element.
If a node is a text node, get the data as a string, split it on words, reverse each one, then assign it back.
If a node is an element, recurse into your function.
Use preg_match_all() function.
$originalStr = "The quick <font color='brown'>brown</font> fox jumps over the lazy dog";
preg_match_all('|<[^>]+>(.*)</[^>]+>|U', $originalStr, $matches, PREG_SET_ORDER, 0);
$_tag = $matches[0][0];
$_txt = $matches[0][1];
$newString = str_replace($_tag,$_txt,$originalStr);

PHP Split array at space before certain qty of characters

I have an application that can only deal with text up to 100 characters in length per line.
However I do not want to be splitting mid-word in a sentence as that doesn't look very nice. Therefore we would need to find the space before the 100th Character and then add it into the array.
I was thinking using strrpos would work - but am unsure how to do the continuing so it has everything in one array
$textToDraw = 'this is a message that is over 100 characters long just to see how well that the breaks work';
$characterLimit = substr($textToDraw, 0, 100);
$textBeforeLimit = strrpos($characterLimit, ' ', 0);
Thanks
UPDATE. This is the current code I have to split the text into an array and then draw each line. However I need it to cut on the space before 100 characters - and not on a hardcoded 100 character limit.
for ($i = 0; $i < count($textToDraw); $i++) {
$splitPoint = 100;
if ( strlen($textToDraw[$i]) > $splitPoint ) {
$newTextLines = str_split($textToDraw[$i], $splitPoint);
array_splice($textToDraw, $i, 1, $newTextLines);
$i = $i + count($newTextLines) - 1;
}
}
foreach ($textToDraw as $actualTextToDraw) {
$page->drawText($actualTextToDraw, $this->x , $this->y , 'UTF-8');
}
You can use php function wordwrap as below. This Wraps a string to a given number of characters using a string break character.
<?php
$textToDraw = 'this is a message that is over 100 characters long just to see how well that the breaks work';
$newtext = wordwrap($textToDraw, 100, "<br />\n");
echo $newtext;
?>
Try wordwrap().
Wraps a string to a given number of characters using a string break character.
Example adapted from the documentation page:
<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
// Outputs:
// The quick brown fox<br />
// jumped over the lazy<br />
// dog.
Update for new info in question:
Apply it to your needs:
$text = "Here's some example text that may or may not be really really long.";
$linedText = wordwrap($text, 20, "\n");
$lines = explode("\n", $linedText);
// Do whatever with $lines.

how can I count number of occurance of a word in a string while ignoring cases

I am trying to count the number of occurance of a given word while ignoring cases.
I have tried
<?php
$string = 'Hello World! EARTh in earth and EARth';//string to look into.
if(stristr($string, 'eartH')) {
echo 'Found';// this just show eartH was found.
}
$timesfound = substr_count($string, stristr($string, 'eartH'));// this count how many times.
echo $timesfound; // this outputs 1 instead of 3.
Lowercase the string before searching:
$string = 'Hello World! EARTh in earth and EARth';
$search = 'EArtH';
var_dump(substr_count(strtolower($string), strtolower($search))); // outputs 3
try substr_count()
<?php
$text = 'This is a test';
echo strlen($text); // 14
echo substr_count($text, 'is'); // 2
?>

Remove ellipsis (&helip;) after sub-string concatenation

I am concatenating two sub-strings with an ellipsis (...) added at the end of the first sub-string. However I want this elipsis to be removed after the concatenation.
Is this possible via a script or other means, Thanks:
<?php echo substr($post_text_result2, 0, 400) . "…";?><div id="second_post" class = "hidden"><?php echo substr($post_text_result2, 400, 5000);?></div>:
str_replace deletes the elipsis like requested, however does not fully work in my situation:
The code below works however, The first sub-string is repeated. I need a way to remove the first sub-string.
<?php
$string = substr($post_text_result2, 0, 400) . "…";
echo $string;
?>
<div id="second_post" class = "hidden">
<?php
$string= str_replace('…','',$string); echo $string;
echo $string;
echo substr($post_text_result2, 400, 5000);
?>
easy
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string);
?>
If you want an ellipsis always at the end, just do this:
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string) . "…";
?>
You can store the length of the first string A and then build a new string concatenating 2 substrings:
substring 1 from 0 until length(A) - 3
substring 2 from length(A) until total length
You also could use a regex and replace "..." with "", but that could remove other "..." which is maybe unwanted.
Sorry for no code, I can't PHP but you know how to do it ;)

PHP Wordwrap on an angle, increasing indentation each break

is it possible with php's wordwrap to add increased indentation each line break to essentially create wordwrapping on an angle?
If I understand your question correcly, you would like to produce an output like:
xxxxxxx
xxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxxxx
Obviously, replacing x with your text.
wordwrap built-in function does not support this feature but you still can write your own, with a simple loop. Change the max length on each iteration, and break your initial string (depending on your needs, where you find a space or wherever you want).
<?php
$text = "The quick brown fox jumped over the lazy dog.";
echo $return = costomwrap($text , 10);
function costomwrap($text , $len)
{
$str = '';
for($i=0;$i<strlen($text);$i = $i+$len)
{
$str .= substr($text , $i , $len ).'<br />';
$len--;
}
return $str;
}
?>
live demo http://codepad.org/v3ysqV5A.
here, <br /> not come on your programme.

Categories