Reputation: 425
My string looks like this:
http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpghttp://localhost/layerthemes/wp-content/uploads/2014/05/Eddy-Need-Remix-mp3-image.jpghttp://localhost/layerthemes/wp-content/uploads/2013/03/static-pages.png
How do I extract each urls in array like this:
array(
0 => 'http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpg'
1 => 'http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpg'
2 => 'http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpg'
)
This is how i tried with no avail:
$imgss = 'http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpghttp://localhost/layerthemes/wp-content/uploads/2014/05/Eddy-Need-Remix-mp3-image.jpghttp://localhost/layerthemes/wp-content/uploads/2013/03/static-pages.png';
preg_match_all(
"#((?:[\w-]+://?|[\w\d]+[.])[^\s()<>]+[.](?:\([\w\d]+\)|(?:[^`!()\[\]{};:'\".,<>?«»“”‘’\s]|(?:[:]\d+)?/?)+))#",
$imgss
);
foreach($imgss as $imgs){
echo '<img src="'.$imgs.'" />';
}
Any help would be appreciated. needless to say I am very weak in php
Thanks
Upvotes: 1
Views: 189
Reputation: 46610
Exploding is fine but perhaps you should also validate the inputted links, ive put together this which will let you know the inputted links need to be on a new line or have a space between them, then it will validate the links and create a new array of valid links that you can then do something with.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' & !empty($_POST['links'])){
//replace all \r\n and \n and space with , delimiter
$links = str_replace(array(PHP_EOL, "\r\n", " "), ',', $_POST['links']);
//explode using ,
$links = explode(',', $links);
//validate links by going through the array
foreach($links as $link){
//does the link contain more then one http://
if(substr_count($link, 'http://') >1){
$error[] = 'Add each url on a new line or separate with a space.';
}else{
//does the link pass validation
if(!filter_var($link, FILTER_VALIDATE_URL)){
$error[] = 'Invalid url skipping: '.htmlentities($link);
}else{
//does the link contain http or https
$scheme = parse_url($link, PHP_URL_SCHEME);
if($scheme == 'http' || $scheme == 'https'){
//yes alls good, add to valid links array
$valid_links[] = $link;
}else{
$error[] = 'Invalid url skipping: '.htmlentities($link);
}
}
}
}
//show whats wrong
if(!empty($error)){
echo '
<pre>
'.print_r($error, true).'
</pre>';
}
//your valid links do somthing
if(!empty($valid_links)){
echo '
<pre>
'.print_r($valid_links, true).'
</pre>';
}
}?>
<form method="POST" action="">
<textarea rows="2" name="links" cols="50"><?php echo (isset($_POST['links']) ? htmlentities($_POST['links']) : null);?></textarea><input type="submit" value="Submit">
</form>
Perhaps it will help.
Upvotes: 1
Reputation: 6965
Here's an example, if you have control over this entire process.
Your form:
<form id="myform" method="POST">
</form>
Your javascript (using jquery):
<script>
var myurls = getUrls();
$('<input>').attr({
type: 'hidden',
name: 'myurls',
value: JSON.stringify(myurls),
}).appendTo('#myform');
// gathers your URLs (however you do this) and returns them as a javascript array
function getUrls() {
// just return this as a placeholder/example
return ["http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpg", "http://localhost/layerthemes/wp-content/uploads/2014/05/Eddy-Need-Remix-mp3-image.jpg", "http://localhost/layerthemes/wp-content/uploads/2013/03/static-pages.png"];
}
</script>
Your PHP:
$myurls = json_decode($_POST['myurls']);
var_dump($myurls); // should be the array you sent
You could do this with AJAX too if you want. Or make the form automatically submit.
Upvotes: 0
Reputation: 12699
If there are no spaces in string you can use:
$string = 'http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpghttp://localhost/layerthemes/wp-content/uploads/2014/05/Eddy-Need-Remix-mp3-image.jpghttp://localhost/layerthemes/wp-content/uploads/2013/03/static-pages.png';
$string = str_replace( 'http', ' http', $string );
$array = array_filter( explode( ' ', $string ) );
print_r( $array );
Upvotes: 4
Reputation: 2282
How about:
$input = "http://localhost/layerthemes/wp-content/uploads/2014/05/46430454_Subscription_XXL-4_mini.jpghttp://localhost/layerthemes/wp-content/uploads/2014/05/Eddy-Need-Remix-mp3-image.jpghttp://localhost/layerthemes/wp-content/uploads/2013/03/static-pages.png";
$exploded = explode("http://", $input);
$result;
for ($i = 1; $i < count($exploded); ++$i)
{
$result[$i - 1] = "http://" . $exploded[$i];
}
Upvotes: 0