preg_split() is a PHP function which allows splitting a string using a regular expression.
Questions tagged [preg-split]
550 questions
2
votes
6 answers
How to break down a string inside array into more arrays PHP
Basically I have an array with several string as values (not associative array). I need to get particular parts of each string and transform it into an array with two values.
Here's the structure of my array:
$data = array(
"A1 -- B1",
"A2 --…

WagnerMatosUK
- 4,309
- 7
- 56
- 95
2
votes
1 answer
Mysqli_real_escape_string preventing preg_split from working?
Example Text: This will be stored in the $_POST['x'] variable
This is sentence one.
This is sentence two.
This is sentence three.
If I run this code below It will return an array with only one element
$x= mysqli_real_escape_string($db,…

jakecraige
- 2,707
- 4
- 21
- 25
2
votes
1 answer
preg_split values between specific tag
Good day.
I'm having a problem with a pattern.
Let's assume I have a string like this:
Hello [person]Name[/person], I don't know regex like
[person]Another_name[/person] does.
I need to preg_split this string to get an array like this:
Array(0…

Maxim R
- 78
- 1
- 6
2
votes
5 answers
PHP preg_split if not inside curly brackets
I'm makin' a scripting language interpreter using PHP. I have this code in that scripting language:
write {Hello, World!} in either the color {blue} or {red} or {#00AA00} and in either the font {Arial Black} or {Monaco} where both the color and the…
user142019
2
votes
5 answers
How can I adapt my regex to allow for escaped quotes?
Introduction
First my general issue is that I want to string replace question marks in a string, but only when they are not quoted. So I found a similar answer on SO (link) and began testing out the code. Unfortunately, of course, the code does not…

Treffynnon
- 21,365
- 6
- 65
- 98
2
votes
3 answers
PHP preg_split result not correct
I am trying to learn regex in PHP and messing around with the preg_split function.
It doesn't appear to be correct though, or my understanding is completely wrong.
The test code i am using is:
$string = "test ing ";
var_dump(preg_split('/t/',…

user1783931
- 173
- 1
- 1
- 7
2
votes
2 answers
How to Split a string via regular expressions and keep the pattern in output?
I have some code here:
$testString = "text23hello54stack90overflow34test";
$testArray = preg_split("/[0-9]{2}/Uim", $testString);
echo "
".print_r($testArray).""; After execution of these commands i have an array containing: {text, hello,…

LostPhysx
- 3,573
- 8
- 43
- 73
2
votes
1 answer
$1 in subject string when using preg_split
I want to split a text into an array where the delimiter is one or more of "\n", and then put the content of the array as elements in a unorder html-list. When I do this using preg_split when there is a $1 in the subject string I get a weird result.…

numfar
- 1,637
- 4
- 18
- 39
2
votes
2 answers
Returning whole string with preg_split
I'm trying to split a string by one or more spaces, but the below isn't working..instead it is returning the whole string as a singular array.
$str = 'I am a test';
$parts = preg_split('/\s+/', $str, PREG_SPLIT_NO_EMPTY);
print_r($parts);
Here is…

Brett
- 19,449
- 54
- 157
- 290
1
vote
3 answers
PHP preg_split get rid of trailing spaces in just one line?
How can i get rid of trailing spaces in preg_split result without using preg_replace to first remove all spaces from $test string?
$test = 'One , Two, Thee ';
$test = preg_replace('/\s+/', ' ', $test);
$pieces = preg_split("/[,]/", $test);
user1098965
1
vote
2 answers
when substring is found, create a DOM element -php
i want to split a string with regex, then create a dom element where i found the match, and do that till string ends.
given a string;
$str="hi there! [1], how are you? [2]";
DESIRED RESULT:
hi there! 1 , how are you?…

teutara
- 605
- 4
- 12
- 24
1
vote
1 answer
PHP preg_split Not Working
please refer to the code below.
$dArr = '';
$dArr gets the value of volunteerDist. echo $dArr prints the value 4.1,9.4,2.3,4.7,9.1,14.7,3.2,7.1,4.1,0.5
I wanted to split this into elements in an Array…

Brandon Young
- 523
- 2
- 8
- 19
1
vote
2 answers
php split string into array by date (yyyy-mm-dd)
I have a plain text journal I'm trying to format in php. I'd like to split each entry into an array containing date and text.
Using preg_split I can do this, however it removes the date altogether!
The txt file looks something like…

Mike Thrussell
- 4,175
- 8
- 43
- 59
1
vote
1 answer
Print array from a preg_split
Hi I trying to print the content of a array.
This is the code:
while ($row = mysql_fetch_assoc($result)) {
$pattern = '[]*>(.*?) ]';
$content = preg_split( $pattern, $row['introtext'], NULL );
echo '';
echo '

Jean
- 5,201
- 11
- 51
- 87
1
vote
2 answers
Split string on dots not preceded by a digit without losing digit in split
Given the following sentence:
The is 10. way of doing this. And this is 43. street.
I want preg_split() to give this:
Array (
[0] => "This is 10. way of doing this"
[1] => "And this is 43. street"
)
I am using:
preg_split("/[^\d+]\./i",…

yasar
- 13,158
- 28
- 95
- 160