I wanted to split a large text into 10 pieces (somehow equal parts).
Ii use this function:
<?php
function chunk($msg) {
$msg = preg_replace('/[\r\n]+/', ' ', $msg);
//define character length of each text piece
$chunks = wordwrap($msg, 10000, '\n');
return explode('\n', $chunks);
}
$arrayys=chunk($t);
foreach($arrayys as $partt){echo $partt."<br/><br/><br/>";}
?>
But is it possible to define word length of each text piece (not character length )? how to divide text into words in such situation?
I would suggest to use "explode"
http://php.net/manual/en/function.explode.php
for splitting the string by spaces. Then you'll get an array of words on which you can iterate and build your text-parts.
From docs,
<?php
$text = "ABCDEFGHIJK.";
$newtext = wordwrap($text,3,"\n",true);
echo "$newtext\n";
?>
OUTPUT: ABC DEF GHI JK.
You can do something like this. Breaks your text into equal parts.. The text in $str is of 20 chars, So the text is broken into 10 parts with 2 chars as a set.
Say, if your large text is of 1000 characters, then you will be getting 100 equal parts of text.
<?php
$div=10;//Equally split into 10 ...
$str="abcdefghijklmnopqrst";
print_r(array_chunk(str_split($str), (strlen($str)/($div))));
OUTPUT:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
[1] => f
)
[3] => Array
(
[0] => g
[1] => h
)
[4] => Array
(
[0] => i
[1] => j
)
[5] => Array
(
[0] => k
[1] => l
)
[6] => Array
(
[0] => m
[1] => n
)
[7] => Array
(
[0] => o
[1] => p
)
[8] => Array
(
[0] => q
[1] => r
)
[9] => Array
(
[0] => s
[1] => t
)
)
Related
I'm trying to add a string to a 3*x Array. I have a string as an input with 150*3 values.
<?php
$myString = "5.1,3.5,Red,4.9,3,Blue,4.7,3.2,Red,4.6,3.1,Red,5,3.6,Red," //and so on
?>
the result should look like
Array
(
[0] => Array
(
[0] => 5.1
[1] => 3.5
[2] => Red
)
[1] => Array
(
[0] => 4.9
[1] => 3
[2] => Blue
)
//and so on
)
First, you will need to convert the comma separated string into an array. Then you can use the array_chunk() function.
$myString = "5.1,3.5,Red,4.9,3,Blue,4.7,3.2,Red,4.6,3.1,Red,5,3.6,Red";
$explodedStringToArray = explode(',', $myString);
$chunked_array = array_chunk($explodedStringToArray, 3);
print_r($chunked_array);
This will produce:
Array
(
[0] => Array
(
[0] => 5.1
[1] => 3.5
[2] => Red
)
[1] => Array
(
[0] => 4.9
[1] => 3
[2] => Blue
)
[2] => Array
(
[0] => 4.7
[1] => 3.2
[2] => Red
)
[3] => Array
(
[0] => 4.6
[1] => 3.1
[2] => Red
)
[4] => Array
(
[0] => 5
[1] => 3.6
[2] => Red
)
)
You can use explode() on the string, and then use array_chunk() to chunk the array we have from explode function, keep in mind to check for the chunk size
working snippet: https://3v4l.org/qD1t0
<?php
$myString = "5.1,3.5,Red,4.9,3,Blue,4.7,3.2,Red,4.6,3.1,Red,5,3.6,Red"; //and so on
$arr = explode(",", $myString);
$chunks = array_chunk($Arr, 3);
print_r($chunks);
$str1 = '10 sold';
$re = "/(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)/";
preg_match_all($re, $str1, $str1matches);
echo print_r($str1matches,1);
prints:
Array
(
[0] => Array
(
[0] => 10
[1] =>
[2] => sold
[3] =>
)
[Alpha] => Array
(
[0] =>
[1] =>
[2] => sold
[3] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] => sold
[3] =>
)
[Numeric] => Array
(
[0] => 10
[1] =>
[2] =>
[3] =>
)
[2] => Array
(
[0] => 10
[1] =>
[2] =>
[3] =>
)
)
But why does it print such a long array, and how do I determine at which position will my values (xxx and label) be available always?
I'd use a simple /^([0-9]+)\s*([a-zA-Z]+)$/ regex since you confirm there is a number and then a word in the input string:
preg_match('/^([0-9]+)\s*([a-zA-Z]+)$/', '10 sold', $str1matches, PREG_OFFSET_CAPTURE);
See the PHP demo:
$str1 = '10 sold';
$re = "/^([0-9]+)\s*([a-zA-Z]+)$/";
preg_match($re, $str1, $str1matches, PREG_OFFSET_CAPTURE);
echo print_r($str1matches[1]);
echo print_r($str1matches[2]);
The $str1matches[1] will contain an array with the Group 1 (number) value and its position, and the $str1matches[2] will contain an array with the Group 2 (word) value and its position.
How can I get Array with all string.
$str = "This is some a text with [b]Bold[/b] and [i]Italic[/i] elements inside";
preg_match_all("/.*(\[.+\]).*/isU",$str,$matches);
print_r($matches);
I obtain only:
Array (
[0] => Array
(
[0] => This is a text with [b]
[1] => Bold[/b]
[2] => and [i]
[3] => Italic[/i]
)
[1] => Array
(
[0] => [b]
[1] => [/b]
[2] => [i]
[3] => [/i]
)
)
without "elements inside" text in the end.
Preg_replace?
$new_str = preg_replace("/(\[.*?\])/", "", $str);
http://www.phpliveregex.com/p/fKH
If you want to print arbitrary pages on windows/office you can define it like in the picture:
So, this will print pages: 1,2,3,6,7,8
Now, I'm trying to do same thing using Regex
<?php
$str = "1-4,6,7,8";
preg_match('/((\d+-\d+)|(\d+)),((\d+-\d+)|(\d+))/',$str,$out);
print_r($out);
?>
and it prints
Array ( [0] => 1-4,6 [1] => 1-4 [2] => 1-4 [3] => [4] => 6 [5] => [6] => 6 )
but I want to is the following
Array ( [0] => 1-4 [1] => 6, [2] => 7, [3] => 7 )
How can I do this?
Here is the fiddle
Check this regexp pattern, please
$str = "1-4,6,7,8";
preg_match('/((\d+-\d+)|(\d+)),?/',$str,$out);
print_r($out);
or better use explode function:
$str = "1-4,6,7,8";
$out = explode(',', $str);
print_r($out);
Use this:
$str = "1-4,6,7,8";
preg_match_all('/(\d+(?:-\d+)?),?/', $str, $out);
print_r($out);
output:
Array
(
[0] => Array
(
[0] => 1-4,
[1] => 6,
[2] => 7,
[3] => 8
)
[1] => Array
(
[0] => 1-4
[1] => 6
[2] => 7
[3] => 8
)
)
This should do the trick:
(\d+)-?(\d*)?(,(?!$))?
Matches 1 or more numbers (mandatory).
Optional match for hyphen.
Optional match for second set of digits.
Optional comma after each set of numbers but does not allow comma on the end.
DEMO
I am using preg_match_all to filter out strings
The string which I have supplied in preg_match_all is
$text = "Friedric'h Wöhler"
after that I use
preg_match_all('/(\"[^"]+\"|[\\p{L}\\p{N}\\*\\-\\.\\?]+)/', $text, $arr, PREG_PATTERN_ORDER);
and the result i get when I print $arr is
Array
(
[0] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
[1] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
)
Somehow the ö character is replaced by ouml which I am not really sure how to figure this out
I am expecting following result
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
Per nhahtdh's comment:
$text = "Friedric'h Wöhler";
preg_match_all('/"[^"]+"|[\p{L}\p{N}*.?\\\'-]+/u', $text, $arr, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($arr);
echo "</pre>";
Gives
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
If you think preg_match_all() is messy, you could take a look at pattern():
$p = '"[^"]+"|[\p{L}\p{N}*.?\\\'-]+'; // automatic delimiters
$text = "Friedric'h Wöhler";
$result = pattern($p)->match($text)->all();