Consider this code (http://codepad.org/lJGcW7tU):
$str = '1
2
3
4
5
6';
var_dump(explode("\n", $str));
I would expect output like this:
array(6) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
[5]=>
string(1) "6"
}
But actually, it's this:
array(6) {
[0]=>
string(2) "1
"
[1]=>
string(2) "2
"
[2]=>
string(2) "3
"
[3]=>
string(2) "4
"
[4]=>
string(2) "5
"
[5]=>
string(1) "6"
}
The explode seems to have added an extra character to all new array elements, except the last one. This character is not there in the original string, so where did it come from? And why?
A line break is represented differently among different platforms. In Unix-systems, it's simply "\n", while on Windows-based systems it's "\r\n".
Your String probably contains "1\r\n2\r\n3\r\n(...)" which means that when you split it on "\n" the first index of the result is "1\r", the second is "2\r" and so on...
Different possible solutions:
Initialize your string to $str = "1\n2\n3\n4\n5\n6";
Write your PHP file in a text editor that uses Unix-based newlines (some more advanced text editors have a setting for which type of line break to use)
Split on "\r\n" instead of just "\n"
Related
i have some strings result of var_dump like this :
string(5) "tes37"
string(6) "SRV410"
string(6) "SRV400"
string(6) "SRV311"
string(6) "SRV302"
string(6) "SRV301"
string(6) "SRV300"
string(6) "SRV001"
string(7) "SRV_123"
string(5) "sad34"
string(7) "S 0001J"
string(8) "S 00004J"
string(8) "S 00003J"
string(8) "S 00002J"
string(6) "asdasd"
string(4) "4356"
string(3) "234"
here i want to get string with regex such as :
S 0001J, S 00002J, S 00003J and S00004J
anyone can help me out to make the pattern regex ?
Thank you
If you don't care about the number of zeros in the string, you can use the following regex:
S 0+[1-9]J
Change the + to a quantifier of the form {number, number} if you want to keep the number of zeros within a specific number. For example, if you want between three to five zeros, you can do S 0{3,5}[1-9]J.
Of course, this is assuming the string is following the format S (a number of zeros)(a digit)J
I need to extract a string that is enclosed by both parentheses and single quotes. Currently, I am using two regex pattern to do the job. With the first regex I retrieve a string from parentheses while the string still contains single quotes, and with the second regex I can strip that single quotes from it. Now, I would like to do this job in a single step. For the past one hour and so I have been experimenting with some patterns without any viable results; may be its due to my limited regex knowledge. So, any feedback you offer to me will be very helpful. I also welcome any solutions apart from regular expressions.
Here is an example string that needs to be parsed.
$string = "[('minute stroller workout', 9.0), ('week', 1.0), ('leaving', 1.0), ('times', 1.0), ('guilt', 1.0), ('baby', 1.0), ('beginning', 1.0)]";
# Strip parentheses
preg_match_all('#\((.*?)\)#', $string, $match);
# I am using the first match here
echo $match[1][0]; // output = 'minute stroller workout', 9.0
# Strip single quotes and extract the string
preg_match('~(["\'])([^"\']+)\1~', $match[1][0], $matches);
echo $matches[2]; // output = minute stroller workout (i.e. what we are looking for)
If I understand you correctly
preg_match_all('/\(\'([\s\w]*)\', ([\d.]*)\)/', $string, $match);
Output for your string
array(3) {
[0]=>
array(7) {
[0]=>
string(32) "('minute stroller workout', 9.0)"
[1]=>
string(13) "('week', 1.0)"
[2]=>
string(16) "('leaving', 1.0)"
[3]=>
string(14) "('times', 1.0)"
[4]=>
string(14) "('guilt', 1.0)"
[5]=>
string(13) "('baby', 1.0)"
[6]=>
string(18) "('beginning', 1.0)"
}
[1]=>
array(7) {
[0]=>
string(23) "minute stroller workout"
[1]=>
string(4) "week"
[2]=>
string(7) "leaving"
[3]=>
string(5) "times"
[4]=>
string(5) "guilt"
[5]=>
string(4) "baby"
[6]=>
string(9) "beginning"
}
[2]=>
array(7) {
[0]=>
string(3) "9.0"
[1]=>
string(3) "1.0"
[2]=>
string(3) "1.0"
[3]=>
string(3) "1.0"
[4]=>
string(3) "1.0"
[5]=>
string(3) "1.0"
[6]=>
string(3) "1.0"
}
}
You can use this single regex:
preg_match("#\('([^']+)#", $string, $matches);
echo $matches[1];
//=> minute stroller workout
I want to grab all IDs (integers) from several URLs within a text. These URLs could look like these:
http://url.tld/index.php/p1
http://url.tld/p2#abc
http://url.tld/index.php/Page/3-xxx
http://url.tld/Page/4
For this, I've built two regexes (the URLs are enclosed by an URL bbcode):
\[url\](http\://url\.tld/index\.php/p(\d+).*?\)[/url\]
\[url\](http\://url\.tld(?:/index\.php)?/Page/(\d+).*?\)[/url\]
However, if i do a preg_match_all with every single regex, I get an array that looks like this (and which is correct):
array(3) {
[0]=>
array(2) {
[0]=>
string(62) "[url]http://url.tld/index.php/Page/6-fdgfh/[/url]"
[1]=>
string(50) "[url]http://url.tld/Page/7[/url]"
}
[1]=>
array(2) {
[0]=>
string(51) "http://url.tld/index.php/Page/6-fdgfh/"
[1]=>
string(39) "http://url.tld/Page/7"
}
[2]=>
array(2) {
[0]=>
string(1) "6"
[1]=>
string(1) "7"
}
}
But if I combine both regexes with a pipe:
\[url\](http\://url\.tld/index\.php/p(\d+).*?|http\://url\.tld(?:/index\.php)?/Page/(\d+).*?)\[/url\]
it builds an array like this (which is wrong):
array(4) {
[0]=>
array(3) {
[0]=>
string(71) "[url]http://url.tld/index.php/p9-abc#hashtag[/url]"
[1]=>
string(62) "[url]http://url.tld/index.php/Page/6-fdgfh/[/url]"
[2]=>
string(50) "[url]http://url.tld/Page/7[/url]"
}
[1]=>
array(3) {
[0]=>
string(60) "http://url.tld/index.php/t9-abc#hashtag"
[1]=>
string(51) "http://url.tld/index.php/Page/6-fdgfh/"
[2]=>
string(39) "http://url.tld/Page/7"
}
[2]=>
array(3) {
[0]=>
string(1) "9"
[1]=>
string(0) ""
[2]=>
string(0) ""
}
[3]=>
array(3) {
[0]=>
string(0) ""
[1]=>
string(1) "6"
[2]=>
string(1) "7"
}
}
====
So, my question is: How can I fix this? What I need is the array structure from the first example, while using both regular expressions as one regular expression, because I need a consistent structure to do a preg_replace_callback later.
I think you're looking for the Branch Reset group:
\[url]((?|http://url\.tld/index\.php/p(\d+).*?|http://url\.tld(?:/index\.php)?/Page/(\d+).*?))\[/url]
Or, for the line-noise-challenged among us:
\[url]
(
(?|
http://url\.tld/index\.php/p(\d+)[^[]*
|
http://url\.tld(?:/index\.php)?/Page/(\d+)[^[]*
)
)
\[/url]
This captures the numbers in group #2, no matter which part of the regex matched it. The whole URL is still captured in group #1.
I have this string:
a[0]=a[27%a.length];
and this pattern
([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)+\%+([a-z])+\.length
Preg match array is this one:
array(6) {
[0]=>
string(18) "a[0]=a[27%a.length"
[1]=>
string(1) "a"
[2]=>
string(1) "0"
[3]=>
string(1) "a"
[4]=>
string(0) ""
[5]=>
string(1) "a"
}
Why is the element 4 empty instead of holding the 27?
I think there's one "+" too many. This regex seems to work as you would expect:
([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)\%+([a-z])+\.length
The following is even cleaner, and it matches the input string:
([a-z])+\[(\S)+\]\=([a-z])+\[(.*?)\%([a-z])+\.length
The difference is that I removed the unnecessary "+" after the squared brackets ("+" means "one or more", while there should be only one bracket in each position).
For an explanation, please refer to regex101, which has a nice formatting for the various parts of the expression: http://regex101.com/r/iB6nH1
Because you used (.*?)+, the matched part will be replaced by empty string at end.
Remove that + then you will get 27 in the match part.
php > preg_match('/([a-z])+\[(\S)+\]\=([a-z])+\[+(.*?)\%+([a-z])+\.length/', $str, $matches);
php > var_dump($matches);
array(6) {
[0]=>
string(18) "a[0]=a[27%a.length"
[1]=>
string(1) "a"
[2]=>
string(1) "0"
[3]=>
string(1) "a"
[4]=>
string(2) "27"
[5]=>
string(1) "a"
}
I have a form input field that accepts multiple "tags" from a user, a bit like the one on this site! So, for example a user could enter something like:
php mysql regex
...which would be nice & simple to separate up the multiple tags, as I could explode() on the spaces. I would end up with:
array('php', 'mysql', 'regex')
However things get a little more complicated as the user can separate tags with commas or
spaces & use double quotes for multi-word tags.
So a user could also input:
php "mysql" regex, "zend framework", another "a, tag with punc $^&!)(123 *note the comma"
All of which would be valid. This should produce:
array('php', 'mysql', 'regex', 'zend framework', 'another', 'a, tag with punc $^&!)(123 *note the comma')
I don't know how to write a regular expression that would firstly match everything in double quotes, then explode the string on commas or spaces & finally match everything else. I guess I would use preg_match_all() for this?
Could anyone point me in the right direction!? Many thanks.
Try this regex out. I tested it against your string, and it correctly pulled out the individual tags:
("([^"]+)"|\s*([^,"\s]+),?\s*)
This code:
$string = 'php "mysql" regex, "zend framework", another "a, tag with punc $^&!)(123 *note the comma"';
$re = '("([^"]+)"|\s*([^,"\s]+),?\s*)';
$matches = array();
preg_match_all($re, $string, $matches);
var_dump($matches);
Yielded the following result for me:
array(3) {
[0]=>
array(6) {
[0]=>
string(4) "php "
[1]=>
string(7) ""mysql""
[2]=>
string(8) " regex, "
[3]=>
string(16) ""zend framework""
[4]=>
string(9) " another "
[5]=>
string(44) ""a, tag with punc $^&!)(123 *note the comma""
}
[1]=>
array(6) {
[0]=>
string(0) ""
[1]=>
string(5) "mysql"
[2]=>
string(0) ""
[3]=>
string(14) "zend framework"
[4]=>
string(0) ""
[5]=>
string(42) "a, tag with punc $^&!)(123 *note the comma"
}
[2]=>
array(6) {
[0]=>
string(3) "php"
[1]=>
string(0) ""
[2]=>
string(5) "regex"
[3]=>
string(0) ""
[4]=>
string(7) "another"
[5]=>
string(0) ""
}
}
Hope that helps.