I have this list of urls:
http://test1.google.com/test1/12345http://test2.google.com/test2/12345http://test3.google.com/test4/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345
It's just an example, I want to preg_match_all a list of valid urls that doesn't have space to seperate between them, so, I will get it in an array and each cell is different url.
No need for preg_match IMHO:
<?php
$links = 'http://test1.google.com/test1/12345http://test2.google.com/test2/12345http://test3.google.com/test4/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345http://test1.google.com/test1/12345';
$links = array_map(function($chunk){return 'http://'.$chunk;}, explode('http://', $links));
array_shift($links);
print_r($links);
Demo, Output:
Array
(
[0] => http://test1.google.com/test1/12345
[1] => http://test2.google.com/test2/12345
[2] => http://test3.google.com/test4/12345
[3] => http://test1.google.com/test1/12345
[4] => http://test1.google.com/test1/12345
[5] => http://test1.google.com/test1/12345
[6] => http://test1.google.com/test1/12345
[7] => http://test1.google.com/test1/12345
)
Related
I am working on retriving category values from a wiki markup text in loop, could not grab category values from the markup using regex match in php
The Markup Text Contains the category values as
$input_wiki_markup = "
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]";
Here's what I have tried so far
$matches = array();
if(preg_match("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
print_r($matches);
}
This is the output
Array
(
[0] => [[Category:Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips]]
[1] => Category
[2] => Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips
)
But I'm trying to get output array with only category values after colon , i.e.
Array
(
[0] => Google
[1] => Tricks
[2] => Google Searcg
)
And so on.
What changes should i make to my regex to get only category values filled up in the $mathces array
Or should i use oter php function instead of preg_match ?
Kindly note that, the $input_wiki_markup also containes other text around the [[Categpry:xyz]] tags
all you need was an all
$input_wiki_markup="
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]
";
$matches = array();
if(preg_match_all("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
print_r($matches);
}
OUTPUT:
Array
(
[0] => Array
(
[0] => [[Category:Google]]
[1] => [[Category:Tricks]]
[2] => [[Category:Google Search]]
[3] => [[Category:Filters]]
[4] => [[Category:Search]]
[5] => [[Category:Tips]]
)
[1] => Array
(
[0] => Category
[1] => Category
[2] => Category
[3] => Category
[4] => Category
[5] => Category
)
[2] => Array
(
[0] => Google
[1] => Tricks
[2] => Google Search
[3] => Filters
[4] => Search
[5] => Tips
)
)
i have this pattern and i ant to use it to extract the numbers after the /image/ field and i have tried this pattern and i have checked online at http://www.functions-online.com/preg_match_all.html and it is giving desired output for the first link but for other links it is not giving desired output
here is my pattern
/\sample.com\/image\/(.*)\//
and here is my string
Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]
eg
i want to extract only number after /image/ field i dont want image name my desired output is
4205476
4453948
427185
etc all numbers from string
Use this Regular Expression ~\/\image\/(.*?)\/~
<?php
$str=' Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]';
preg_match_all('~\/\image\/(.*?)\/~', $str, $matches);
print_r($matches[1]);
OUTPUT :
Array
(
[0] => 4205476
[1] => 4453948
[2] => 427185
[3] => 1323868
[4] => 2505314
[5] => 3300481
[6] => 1046646
[7] => 4322305
[8] => 4261476
[9] => 3386911
[10] => 4663949
)
You need to adjust your regular expression:
$regex = '#sample\.com/image/([0-9]+)/#'
preg_match_all('#sample\.com/image/([0-9]+)/#', $str, $m);
print_r($m);
Expected output:
Array
(
[0] => Array
(
[0] => sample.com/image/4205476/
[1] => sample.com/image/4453948/
[2] => sample.com/image/427185/
[3] => sample.com/image/1323868/
[4] => sample.com/image/2505314/
[5] => sample.com/image/3300481/
[6] => sample.com/image/1046646/
[7] => sample.com/image/4322305/
[8] => sample.com/image/4261476/
[9] => sample.com/image/3386911/
[10] => sample.com/image/4663949/
)
[1] => Array
(
[0] => 4205476
[1] => 4453948
[2] => 427185
[3] => 1323868
[4] => 2505314
[5] => 3300481
[6] => 1046646
[7] => 4322305
[8] => 4261476
[9] => 3386911
[10] => 4663949
)
)
Now you'll need to keep in mind that PHP will return everything it matches including the undesired parts of the regex string.
From the PHP Manual:
http://www.php.net/manual/en/function.preg-match-all.php
Orders results so that $matches[0] is an array of full pattern
matches, $matches[1] is an array of strings matched by the first
parenthesized subpattern, and so on.
Try this:
/.*sample\.com\/image\/(\d+)\/.*/
Debuggex Demo
I have a string which gets exploded into an array using the space as a delimiter. Is it possible to , for example explode the first 4 words into the array and the rest into ONE array element?
as of now the code is like this
$string = 'This is a string that needs to be split into elements';
$splitarray = explode(' ',$string);
This gives an array
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to
[7] => be
[8] => split
[9] => into
[10] => elements
)
What i need is for the array to look like this
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to be split into elements
)
Is something like this possible?
Use limit parameter here.
From explode() documentation:
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
Code:
$string = 'This is a string that needs to be split into elements';
$splitarray = explode(' ',$string, 7);
print_r($splitarray);
Output:
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to be split into elements
)
I am trying to preg_match a url consisting of a category slug, an optional subcategory slug and an option item slug.
It works in all cases, except for the 4th case.
$urls[0] = '/main_cat_slug';
$urls[1] = '/main_cat_slug/';
$urls[2] = '/main_cat_slug/sub_cat_slug';
$urls[3] = '/main_cat_slug/sub_cat_slug/';
$urls[4] = '/main_cat_slug/item.html';
$urls[5] = '/main_cat_slug/sub_cat_slug/item.html';
$regexp = array();
$regexp[] = '/(?:(?<category>[\w]+)/?)'; // Find the main category (is always available)
$regexp[] = '(?:(?<subcategory>[\w]+)/?)?'; // Find an optional sub-category, is not always available
$regexp[] = '(?:(?<item>[\w]+)\.html)?'; // Find an optional item, is not always available (don't catch the extension)
$regexp = implode('', $regexp);
foreach($urls as $index=>$url) {
preg_match("#{$regexp}#i", $url, $matches);
echo '<pre><h1>', $index, '</h1>';
echo $url, '<br />';
echo '<br />';
print_r($matches);
}
In the 4-th case, the category will be found, but the item is empty and the subcategory gets the value op "item".
Could someone help me out, so that the 4-th case will only get a category and an item?
This is the output for above code:
0
/main_cat_slug
Array
(
[0] => /main_cat_slug
[category] => main_cat_slug
[1] => main_cat_slug
)
1
/main_cat_slug/
Array
(
[0] => /main_cat_slug/
[category] => main_cat_slug
[1] => main_cat_slug
)
2
/main_cat_slug/sub_cat_slug
Array
(
[0] => /main_cat_slug/sub_cat_slug
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
)
3
/main_cat_slug/sub_cat_slug/
Array
(
[0] => /main_cat_slug/sub_cat_slug/
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
)
4
/main_cat_slug/item.html
Array
(
[0] => /main_cat_slug/item
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => item
[2] => item
)
5
/main_cat_slug/sub_cat_slug/item.html
Array
(
[0] => /main_cat_slug/sub_cat_slug/item.html
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
[item] => item
[3] => item
)
Kind regards!
Patrick
Description
This regex will pickout the three types of data, using the following rules:
The / is always the first character in the string
The Main_Cat is always first, it follows the first / and continues until the next /
If the first string ends in .html/ then this is a Main_Cat
if the first string ends in .html followed by the end of the string, then this is an item
The Sub_Cat is always second, it follows the second / and continues until the next /
If the second string ends in .html/ then this is a Sub_Cat
if the second string ends in .html followed by the end of the string, then this is an item
The Item type always has an .html suffix
There will never be a / after the Item
the Item type will always be the last field
^\/(?:(?<Main_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Sub_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Item>[^\/\r\n]*?)(?:\.html|$))?
If you're using this expression against individual strings then you can remove the new line characters \r\n. The resulting expression would look like: ^\/(?<Main_Cat>[^\/]*)(?:(?:\/(?![^\/]*\.html)(?<Sub_Cat>[^\/]*))?(?:\/(?<Item>[^\/]*)\.html)?)?.*?$ follows the same rules above. Note the end of line $ forces the test to match your entire string
PHP Code Example:
Source String
/category0.html/subcat/item.html
/item1.html
/category2.html/subcat2.html/item2.html
/category3.html/subcat3.html/
/category4.html/item4.html
/main_cat_slug5.html/
/main_cat_slug6/item6
/main_cat_slug7/sub_cat_slug7.html/
/main_cat_slug8/item8.html
/main_cat_slug9/sub_cat_slug9/item9.html
Code
<?php
$sourcestring="your source string";
preg_match_all('/^\/(?:(?<Main_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Sub_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Item>[^\/\r\n]*?)(?:\.html|$))?/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
Matches
$matches Array:
(
[0] => Array
(
[0] => /category0.html/subcat/item.html
[1] => /item1.html
[2] => /category2.html/subcat2.html/item2.html
[3] => /category3.html/subcat3.html
[4] => /category4.html/item4.html
[5] => /main_cat_slug5.html
[6] => /main_cat_slug6
[7] => /main_cat_slug7/sub_cat_slug7.html
[8] => /main_cat_slug8/item8.html
[9] => /main_cat_slug9/sub_cat_slug9/item9.html
)
[Main_Cat] => Array
(
[0] => category0.html
[1] =>
[2] => category2.html
[3] => category3.html
[4] => category4.html
[5] => main_cat_slug5.html
[6] => main_cat_slug6
[7] => main_cat_slug7
[8] => main_cat_slug8
[9] => main_cat_slug9
)
[Sub_Cat] => Array
(
[0] => subcat
[1] =>
[2] => subcat2.html
[3] => subcat3.html
[4] =>
[5] =>
[6] =>
[7] => sub_cat_slug7.html
[8] =>
[9] => sub_cat_slug9
)
[Item] => Array
(
[0] => item
[1] => item1
[2] => item2
[3] =>
[4] => item4
[5] =>
[6] =>
[7] =>
[8] => item8
[9] => item9
)
)
You can try this:
preg_match('~/(?<main_cat>[^/\s]++/?+)(?<sub_cat>[^/\s]++/?+)?'
. '(?>(?<filename>\S+?)\.html)?~', $url, $match);
print_r($match);
Note that you can access easily to the different parts with the named captures (useful to test if there is a subpattern or not.).
I have for example this string: "iCanSeeBluePeople" and I need it to separate it into array by capital letters and the first word which starts with lowercase so I would recieve array like ["i","Can","See","Blue","People"]
The strings can be like "grandPrix2009" => ["grand","Prix","2009"], "dog" => ["dog"], "aDog" => ["a","Dog"] and so on
I found this code which works fine but I doesn't apply to numbers and ignores the fist lowercase letter:
<?
$str="MustangBlueHeadlining";
preg_match_all('/[A-Z][^A-Z]*/',$str,$results);
?>
Thanks for help
You can use the regex /[a-z]+|[A-Z]+[a-z]*|[0-9]+/.
<?
$str="thisIsATestVariableNumber000";
preg_match_all('/[a-z]+|[A-Z]+[a-z]*|[0-9]+/',$str,$results);
print_r($results);
?>
Result:
Array
(
[0] => Array
(
[0] => this
[1] => Is
[2] => ATest
[3] => Variable
[4] => Number
[5] => 000
)
)
Use /[a-z]+|[A-Z][a-z]*|[0-9]+/ if you want ATest to be separated into A and Test.
<?
$str="thisIsATestVariableNumber000";
preg_match_all('/[a-z]+|[A-Z][a-z]*|[0-9]+/',$str,$results);
print_r($results);
?>
Result:
Array
(
[0] => Array
(
[0] => this
[1] => Is
[2] => A
[3] => Test
[4] => Variable
[5] => Number
[6] => 000
)
)