Extract n-character substring while ignoring spaces - php

I need to extract a substring (for instance 22 characters) but I need to ignore spaces when counting the number of characters. For example:
$para = "While still in high school I signed up to participate in amateur night at the Educational Alliance. I wanted to show my mother I had talent.";
Let's say I need to get the substring that contains the 22 first characters but without counting the spaces. substr doesn't work:
echo substr($para, 0, 22); // => While still in high sc
But I need to get
// => While still in high school
How can I do this?

^(?=((?>.*?\S){20}))
Try this.Grab the capture or group.See demo.
https://regex101.com/r/fM9lY3/42
This uses lookahead to capture 20 groups of any character and a non space character. Precisely,lookahead will search for groups ending with non space character.Because it is non greedy,it will search first such 20 groups.

you just need to provide a string and length you want to be extracted from that string and function will return that string of specified length(yes return string will have spaces in it, but spaces won't be included in string).
Here is snippet.
$para = "While still in high school I signed up to participate in amateur night at the Educational Alliance. I wanted to show my mother I had talent.";
function getString($str, $length){
$newStr = "";
$counter = 0;
$r = array();
for($i=0; $i<strlen($str); $i++)
$r[$i] = $str[$i];
foreach($r as $char){
$newStr .= $char;
if($char != " "){
$counter += 1;
}
//return string if length reached.
if($counter == $length){
return $newStr;
}
}
return $newStr;
}
echo getString($para, 20);
//output: While still in high scho
echo getString($para, 22);
//output: While still in high school

First, use str_replace() to create a string $parawithoutspaces that consists of $para, without the spaces, like so:
$parawithoutspaces=str_replace(" ", "", $para);
Then, use substr() get the first 20 characters of $parawithoutspaces like so:
print substr($parawithoutspaces, 0, 20);
Or, combining the two steps into one and eliminating the need for the intermediate variable $parawithoutspaces:
print substr(str_replace(" ", "", $para),0,20);

You can try this, it is my code, $result is final string you want :
$arr1 = substr( $string,0,20);
$arr1 = explode(" ",$arr1);
array_pop($arr1);
$result = implode(" ",$arr1);

Related

How to cut a text after a number of chars and obtain text sporead on multiple rows?

I have a very long text, and I need to cut the text after N chars, so that at the end I obtain a text, rendered on multiple rows, without any of the words being cut;
So, if a have a text with the lenght of a 1000 chars, which has been saved on 1 line, and I need to cut from 100 to 100 chars, at the end, I will get a text spread on 10 lines.
I tryed something, but I got stuck;
foreach does not work, the text is not seen a a array; also, i did not made sure to keep the words intact in my test;
Has anyone tryed this? Or is there any link with solution?
public static function cut_line_after_n_chars($str, $n = 70) {
$result = '';
$pos = 0;
foreach ($str as $c) {
$pos++;
if ($pos == $n) {
$result .= $c + '<br/>';
$pos = 0;
}
else
$result .= $c;
}
return $result;
}
It sounds like you need wordwrap.
http://php.net/manual/en/function.wordwrap.php
This allows you to break a string into an array of pieces without cutting off words. You can then format these pieces as you like.
EDIT
If you still need each of your lines to be 100 characters, you can use str_pad to add extra spaces onto each row.
Use explode() function to get array of words from your string.
$words = explode( ' ', $str );
$length = 0;
foreach( $words as $word ) {
// Your loop code goes here.
}

Get the current + the next word in a string

this is what I try to get:
My longest text to test When I search for e.g. My I should get My longest
I tried it with this function to get first the complete length of the input and then I search for the ' ' to cut it.
$length = strripos($text, $input) + strlen($input)+2;
$stringpos = strripos($text, ' ', $length);
$newstring = substr($text, 0, strpos($text, ' ', $length));
But this only works first time and then it cuts after the current input, means
My lon is My longest and not My longest text.
How I must change this to get the right result, always getting the next word. Maybe I need a break, but I cannot find the right solution.
UPDATE
Here is my workaround till I find a better solution. As I said working with array functions does not work, since part words should work. So I extended my previous idea a bit. Basic idea is to differ between first time and the next. I improved the code a bit.
function get_title($input, $text) {
$length = strripos($text, $input) + strlen($input);
$stringpos = stripos($text, ' ', $length);
// Find next ' '
$stringpos2 = stripos($text, ' ', $stringpos+1);
if (!$stringpos) {
$newstring = $text;
} else if ($stringpos2) {
$newstring = substr($text, 0, $stringpos2);
} }
Not pretty, but hey it seems to work ^^. Anyway maybe someone of you have a better solution.
You can try using explode
$string = explode(" ", "My longest text to test");
$key = array_search("My", $string);
echo $string[$key] , " " , $string[$key + 1] ;
You can take i to the next level using case insensitive with preg_match_all
$string = "My longest text to test in my school that is very close to mY village" ;
var_dump(__search("My",$string));
Output
array
0 => string 'My longest' (length=10)
1 => string 'my school' (length=9)
2 => string 'mY village' (length=10)
Function used
function __search($search,$string)
{
$result = array();
preg_match_all('/' . preg_quote($search) . '\s+\w+/i', $string, $result);
return $result[0];
}
There are simpler ways to do that. String functions are useful if you don't want to look for something specific, but cut out a pre-defined length of something. Else use a regular expression:
preg_match('/My\s+\w+/', $string, $result);
print $result[0];
Here the My looks for the literal first word. And \s+ for some spaces. While \w+ matches word characters.
This adds some new syntax to learn. But less brittle than workarounds and lengthier string function code to accomplish the same.
An easy method would be to split it on whitespace and grab the current array index plus the next one:
// Word to search for:
$findme = "text";
// Using preg_split() to split on any amount of whitespace
// lowercasing the words, to make the search case-insensitive
$words = preg_split('/\s+/', "My longest text to test");
// Find the word in the array with array_search()
// calling strtolower() with array_map() to search case-insensitively
$idx = array_search(strtolower($findme), array_map('strtolower', $words));
if ($idx !== FALSE) {
// If found, print the word and the following word from the array
// as long as the following one exists.
echo $words[$idx];
if (isset($words[$idx + 1])) {
echo " " . $words[$idx + 1];
}
}
// Prints:
// "text to"

Word counter: Doesn't seem to give the output I need (PHP)

here's the line of code that I came up with:
function Count($text)
{
$WordCount = str_word_count($text);
$TextToArray = explode(" ", $text);
$TextToArray2 = explode(" ", $text);
for($i=0; $i<$WordCount; $i++)
{
$count = substr_count($TextToArray2[$i], $text);
}
echo "Number of {$TextToArray2[$i]} is {$count}";
}
So, what's gonna happen here is that, the user will be entering a text, sentence or paragraph. By using substr_count, I would like to know the number of occurrences of the word inside the array. Unfortunately, the output the is not what I really need. Any suggestions?
I assume that you want an array with the word frequencies.
First off, convert the string to lowercase and remove all punctuation from the text. This way you won't get entries for "But", "but", and "but," but rather just "but" with 3 or more uses.
Second, use str_word_count with a second argument of 2 as Mark Baker says to get a list of words in the text. This will probably be more efficient than my suggestion of preg_split.
Then walk the array and increment the value of the word by one.
foreach($words as $word)
$output[$word] = isset($output[$word]) ? $output[$word] + 1 : 1;
If I had understood your question correctly this should also solve your problem
function Count($text) {
$TextToArray = explode(" ", $text); // get all space separated words
foreach($TextToArray as $needle) {
$count = substr_count($text, $needle); // Get count of a word in the whole text
echo "$needle has occured $count times in the text";
}
}
$WordCounts = array_count_values(str_word_count(strtolower($text),2));
var_dump($WordCounts);

PHP: cutting a string in a number of characters blocks

I have a variable called $data which contains a giant string (27338 characters).
This string will be the content of a pdf file.
I need to add a footnote at the end of each page of the pdf file.
I’ve calculated 2642 characters per page.
How can I cut $data in chunks of 2642 characters so I can add the footnotes?
I'm trying:
$split = preg_split('/(.{0,2642})\s/',
$data,
0,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$count = count($split);
foreach ($split as $key => $page) {
echo $page; echo "<br><br>";
}
But each $page shows much less than 2642 characters.
Thanks a lot
I never used it with very large amount of data but a way to split strings in elements of equal length is to use the str_split function (see http://php.net/str_split)
usage:
$array = str_split('some text', 2);
and you will get an array with elements of length 2 (except for the last one)
You can use the substring-function, which will create a specific substring (of a defined length) from a different string.
substr($STRING, A, B); //where $string is the original string,
//A is the position where to start cutting
//B is how long the newly created string should be (it's optional!)
$text = "ABCDEF";
echo substr($text, 2, 2); //will give you "CD"
this?
define("CHUNK_SIZE",2642);
$string = "big..";
$chunks = array();
for($x=0,$sz=strlen($string);$x<$sz;$x+=CHUNK_SIZE) {
$chunks[] = substr($string,$x,$x+CHUNK_SIZE);
}
$chunks[0..n] += FOOTNOTE
then:
implode("",$chunks);

How can we split a sentence

I have written the PHP code for getting some part of a given dynamic sentence, e.g. "this is a test sentence":
substr($sentence,0,12);
I get the output:
this is a te
But i need it stop as a full word instead of splitting a word:
this is a
How can I do that, remembering that $sentence isn't a fixed string (it could be anything)?
use wordwrap
If you're using PHP4, you can simply use split:
$resultArray = split($sentence, " ");
Every element of the array will be one word. Be careful with punctuation though.
explode would be the recommended method in PHP5:
$resultArray = explode(" ", $sentence);
first. use explode on space. Then, count each part + the total assembled string and if it doesn't go over the limit you concat it onto the string with a space.
Try using explode() function.
In your case:
$expl = explode(" ",$sentence);
You'll get your sentence in an array. First word will be $expl[0], second - $expl[1] and so on. To print it out on the screen use:
$n = 10 //words to print
for ($i=0;$i<=$n;$i++) {
print $expl[$i]." ";
}
Create a function that you can re-use at any time. This will look for the last space if the given string's length is greater than the amount of characters you want to trim.
function niceTrim($str, $trimLen) {
$strLen = strlen($str);
if ($strLen > $trimLen) {
$trimStr = substr($str, 0, $trimLen);
return substr($trimStr, 0, strrpos($trimStr, ' '));
}
return $str;
}
$sentence = "this is a test sentence";
echo niceTrim($sentence, 12);
This will print
this is a
as required.
Hope this is the solution you are looking for!
this is just psudo code not php,
char[] sentence="your_sentence";
string new_constructed_sentence="";
string word="";
for(i=0;i<your_limit;i++){
character=sentence[i];
if(character==' ') {new_constructed_sentence+=word;word="";continue}
word+=character;
}
new_constructed_sentence is what you want!!!

Categories