preg-split

I have a PHP regEx, how do add a condition for the number of characters?

孤街浪徒 提交于 2019-12-04 05:19:47
问题 I have a regular expression that Im using in php: $word_array = preg_split( '/(\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)/', urldecode($path), NULL, PREG_SPLIT_NO_EMPTY ); It works great. It takes a chunk of url paramaters like: /2009/06/pagerank-update.html and returns an array like: array(4) { [0]=> string(4) "2009" [1]=> string(2) "06" [2]=> string(8) "pagerank" [3]=> string(6) "update" } The only thing I need is for it to also not return strings that are

PHP Extract numbers from a string

断了今生、忘了曾经 提交于 2019-12-02 19:50:50
问题 I want to extract numbers from a string in PHP like following : if the string = 'make1to6' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted i will be using these returned values for some calculations.' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted The length of the string is not fixed and can be a max of 10

How to add an ellipsis hyperlink after the first space beyond 170 characters?

允我心安 提交于 2019-12-02 16:16:32
问题 I have a long text like below: $postText="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover

How to add an ellipsis hyperlink after the first space beyond 170 characters?

随声附和 提交于 2019-12-02 13:20:34
I have a long text like below: $postText="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy."; I want to add readmore hyperlink after 170 characters without

preg_split() String into Text and Numbers [duplicate]

假如想象 提交于 2019-12-02 11:10:52
问题 This question already has answers here : Split String into Text and Number (2 answers) Closed 5 years ago . i want to split the string into numbers and text as separate. print_r(preg_split("/[^0-9]+/", "12345hello")); output: Array ( [0] => 12345 [1] => ) 回答1: By using [^0-9]+ you are actually matching the numbers and splitting on them, which leaves you with an empty array element instead of the expected result. You can use a workaround to do this. print_r(preg_split('/\d+\K/', '12345hello'))

preg_split string into letter pairs

岁酱吖の 提交于 2019-12-02 09:45:26
问题 I'm having way too much trouble with this simple problem: split a string into an array of 2-character values, i.e. $string = 'abcdefgh'; // With the correct regex, should return ['ab','cd','ef','gh']; $array = preg_split("/?????/",$string); What's the darn regex? 回答1: Use str_split() instead. $chunks = str_split($string, 2); 回答2: Hint: If you split ON the characters, you end up with an array of 4 elements that are blank eg. /../i I don't think the preg_split is what you want, perhaps preg

Splitting a string on multiple separators in PHP

柔情痞子 提交于 2019-12-02 09:14:25
问题 I can split a string with a comma using preg_split , like $words = preg_split('/[,]/', $string); How can I use a dot, a space and a semicolon to split string with any of these? PS. I couldn't find any relevant example on the PHP preg_split page, that's why I am asking. 回答1: Try this: <?php $string = "foo bar baz; boo, bat"; $words = preg_split('/[,.\s;]+/', $string); var_dump($words); // -> ["foo", "bar", "baz", "boo", "bat"] The Pattern explained [] is a character class, a character class

preg_split string into letter pairs

我的梦境 提交于 2019-12-02 07:18:45
I'm having way too much trouble with this simple problem: split a string into an array of 2-character values, i.e. $string = 'abcdefgh'; // With the correct regex, should return ['ab','cd','ef','gh']; $array = preg_split("/?????/",$string); What's the darn regex? Use str_split() instead. $chunks = str_split($string, 2); Jarret Minkler Hint: If you split ON the characters, you end up with an array of 4 elements that are blank eg. /../i I don't think the preg_split is what you want, perhaps preg_match_all ? eg. $cnt = preg_match_all('/../i', $string, $matches); Sreerac /.{1,2}/ perhaps? Accept

I have a PHP regEx, how do add a condition for the number of characters?

社会主义新天地 提交于 2019-12-02 06:38:24
I have a regular expression that Im using in php: $word_array = preg_split( '/(\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)/', urldecode($path), NULL, PREG_SPLIT_NO_EMPTY ); It works great. It takes a chunk of url paramaters like: /2009/06/pagerank-update.html and returns an array like: array(4) { [0]=> string(4) "2009" [1]=> string(2) "06" [2]=> string(8) "pagerank" [3]=> string(6) "update" } The only thing I need is for it to also not return strings that are less than 3 characters . So the "06" string is garbage and I'm currently using an if statement to weed

Splitting a string on multiple separators in PHP

扶醉桌前 提交于 2019-12-02 05:41:36
I can split a string with a comma using preg_split , like $words = preg_split('/[,]/', $string); How can I use a dot, a space and a semicolon to split string with any of these? PS. I couldn't find any relevant example on the PHP preg_split page, that's why I am asking. Try this: <?php $string = "foo bar baz; boo, bat"; $words = preg_split('/[,.\s;]+/', $string); var_dump($words); // -> ["foo", "bar", "baz", "boo", "bat"] The Pattern explained [] is a character class, a character class consists of multiple characters and matches to one of the characters which are inside the class . matches the