hy I want grab every filename for example :
I have data
01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg
I'm try like this but not working for me
$str = $_POST['name'];
print_r (explode(".jpg", $str));
foreach ($str as $key => $value) {
echo $value.'<br>';
}
The solution using preg_match_all function:
$str = "01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg";
preg_match_all("/(?<=\.jpg)?\w+\.jpg/", $str, $matches);
print_r($matches[0]);
The output:
Array
(
[0] => 01.jpg
[1] => 02.jpg
[2] => 03.jpg
[3] => 10599335_899600036724556_4656814811345726851_n.jpg
[4] => 11693824_1051832718167953_6310308040295800037_n.jpg
[5] => 11709788_1051835281501030_8503525152567309473_n.jpg
[6] => 12042832_1103685106316047_3711793359145824637_n.jpg
)
You can use this lookbehind regex in preg_match_all:
/(?<=^|\.jpg)\w+\.jpg/
Lookbehind asserts that our match has a preceding .jpg or line start.
RegEx Demo
Code:
$re = '/(?<=^|\.jpg)\w+\.jpg/m';
preg_match_all($re, $input, $matches);
print_r($matches[0]);
Code Demo
The explode takes the delimited value off so you would need to re-append it.
$filenames = explode('.jpg', '01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg');
foreach($filenames as $file) {
if(!empty($file)) {
echo $file . ".jpg\n";
}
}
https://eval.in/596532
A regex approach that I think would work is:
(.*?\.jpg)
Regex demo: https://regex101.com/r/gR7rC5/1
PHP:
preg_match_all('/(.*?\.jpg)/', '01.jpg02.jpg03.jpg10599335_899600036724556_4656814811345726851_n.jpg11693824_1051832718167953_6310308040295800037_n.jpg11709788_1051835281501030_8503525152567309473_n.jpg12042832_1103685106316047_3711793359145824637_n.jpg', $filenames);
foreach($filenames[1] as $file) {
echo $file . "\n";
}
Demo: https://eval.in/596530
preg_match_all("/(.*?\.jpg)/", $str, $out);
Var_dump($out[1]);
Click preg_match_all
http://www.phpliveregex.com/p/gcR
Related
I want to remove duplicates image. How to use array_unique? This is what I have tried:
$text = 'text image.jpg flower.jpg txt image.jpg';
$pattern = '/[\w\-]+\.(jpg|png|gif|jpeg)/';
$result = preg_match_all($pattern, $text, $matches);
$matches = $matches[0];
foreach ($matches as $klucz => $image) {
echo $image;
}
array_unique should be applied to an array. So split your string into chunks and use it:
$names = array_unique(explode(' ', $text));
First, explode() the string around " " then call array_unique(). Eg below:
$text = 'text image.jpg flower.jpg txt image.jpg';
$arr = explode(" ", $text);
$arr = array_unique($arr);
print_r($arr); // Array ( [0] => text [1] => image.jpg [2] => flower.jpg [3] => txt )
Read more:
explode()
array_unique()
I used preg_match because in the text the pictures are from the path
$text = 'text image.jpg flower.jpg txt <img src="path/image.jpg" '>;
I'm a bit lost with preg_split() in parsing a string with multiple delimiters and keeping the delimiter in the 'after' part of the split.
My delimiters are $, #, and ?.
For instance:
$str = 'participant-$id#-group';
$ar = preg_split('/([^$#?]+[$#?]+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo "<pre>"; print_r( $ar); echo "</pre>";
will show:
Array
(
[0] => participant_data-$
[1] => id#
[2] => -group
)
However I need:
Array
(
[0] => participant_data-
[1] => $id
[2] => #-group
)
Regex makes my brain hurt. so could someone advise how I use PREG_SPLIT_DELIM_CAPTURE and keep the delimiter at the beginning of the segment?
Try this:
$ar = preg_split('/(\$[^#]+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
How about this. I am capturing the delimiters and then put them back together.
<?php
$str = 'participant-$id#-group';
$ar = preg_split('/([^$#?]+[^$#?]+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo "<pre>"; print_r( $ar); echo "</pre>";
/*
Array
(
[0] => participant-
[1] => $
[2] => id
[3] => #
[4] => -group
) */
$result = array();
$result[] = $ar[0];
for($i=1;$i<count($ar);$i+=2) {
$result[] = $ar[$i] . $ar[$i+1];
}
echo "<pre>"; print_r( $result); echo "</pre>";
/*
Array
(
[0] => participant-
[1] => $id
[2] => #-group
)
*/
?>
You don't need to capture the delimiters, just use a lookahead for the $, #, or ?. This will split your string on the zero-width position before the delimiters. No characters will be lost/consumed why exploding.
Code: (Demo)
$str = 'participant-$id#-group';
var_export(
preg_split('/(?=[$#?])/', $str)
);
Output:
array (
0 => 'participant-',
1 => '$id',
2 => '#-group',
)
Here is my code, which currently does not working properly. How can I make it working? My wish is to make output like one string (of course I know how to "convert" array to string):
words altered, added, and removed to make it
Code:
<?php
header('Content-Type: text/html; charset=utf-8');
$text = explode(" ", strip_tags("words altered added and removed to make it"));
$stack = array();
$words = array("altered", "added", "something");
foreach($words as $keywords){
$check = array_search($keywords, $text);
if($check>(-1)){
$replace = " ".$text[$check].",";
$result = str_replace($text[$check], $replace, $text);
array_push($stack, $result);
}
}
print_r($stack);
?>
Output:
Array
(
[0] => Array
(
[0] => words
[1] => altered,
[2] => added
[3] => and
[4] => removed
[5] => to
[6] => make
[7] => it
)
[1] => Array
(
[0] => words
[1] => altered
[2] => added,
[3] => and
[4] => removed
[5] => to
[6] => make
[7] => it
)
)
Without more explanation it's as simple as this:
$text = strip_tags("words altered added and removed to make it");
$words = array("altered", "added", "something");
$result = $text;
foreach($words as $word) {
$result = str_replace($word, "$word,", $result);
}
Don't explode your source string
Loop the words and replace the word with the word and added comma
Or abandoning the loop approach:
$text = strip_tags("words altered added and removed to make it");
$words = array("altered", "added", "something");
$result = preg_replace('/('.implode('|', $words).')/', '$1,', $text);
Create a pattern by imploding the words on the alternation (OR) operator |
Replace found word with the word $1 and a comma
You can use an iterator
// Array with your stuff.
$array = [];
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $value) {
echo $v, " ";
}
Your original approach should work with a few modifications. Loop over the words in the exploded string instead. For each one, if it is in the array of words to modify, add the comma. If not, don't. Then the modified (or not) word goes on the stack.
$text = explode(" ", strip_tags("words altered added and removed to make it"));
$words = array("altered", "added", "something");
foreach ($text as $word) {
$stack[] = in_array($word, $words) ? "$word," : $word;
}
Given the function:
function getUrlsAndEmails($string) {
$regex = '/(?:[^\s]+#[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|\s)[a-z]+(\.[a-z]+)+(\/[^\s]*)?)/';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
Sometimes return results like:
Array
(
[0] => google.com
[1] => yahoo.com
)
How can I efficiently trim whitespace from all results of a preg_match_all()?
Of course I can loop through all of the results and trim(), but is there a more efficient way than adding this to the function above:
foreach ($matches[0] as $k => $v) {
$matches[0][$k] = trim($v);
}
Try this:
$regex = '/(?:[^\s]+#[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|(?!\s))[a-z]+(\.[a-z]+)+(\/[^\s]*)?)/';
It uses a negative lookahead assertion for the space.
Use map('trim').
<?php
$pattern = Pattern::of('(?:[^\s]+#[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|\s)[a-z]+(\.[a-z]+)+(\/[^\s]*)?)');
$matcher = $pattern->match($string);
var_dump($matcher->map('trim'));
result
Array
(
[0] => 'google.com'
[1] => 'yahoo.com'
)
This question already has answers here:
How to split a string by multiple delimiters in PHP?
(4 answers)
Closed 12 months ago.
Can we do multiple explode() in PHP?
For example, to do this:
foreach(explode(" ",$sms['sms_text']) as $no)
foreach(explode("&",$sms['sms_text']) as $no)
foreach(explode(",",$sms['sms_text']) as $no)
All in one explode like this:
foreach(explode('','&',',',$sms['sms_text']) as $no)
What's the best way to do this? What I want is to split the string on multiple delimiters in one line.
If you're looking to split the string with multiple delimiters, perhaps preg_split would be appropriate.
$parts = preg_split( '/(\s|&|,)/', 'This and&this and,this' );
print_r( $parts );
Which results in:
Array (
[0] => This
[1] => and
[2] => this
[3] => and
[4] => this
)
Here is a great solution I found at PHP.net:
<?php
//$delimiters must be an array.
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);
print_r($exploded);
//And output will be like this:
// Array
// (
// [0] => here is a sample
// [1] => this text
// [2] => and this will be exploded
// [3] => this also
// [4] => this one too
// [5] => )
// )
?>
you can use this
function multipleExplode($delimiters = array(), $string = ''){
$mainDelim=$delimiters[count($delimiters)-1]; // dernier
array_pop($delimiters);
foreach($delimiters as $delimiter){
$string= str_replace($delimiter, $mainDelim, $string);
}
$result= explode($mainDelim, $string);
return $result;
}
You could use preg_split() function to stplit a string using a regular expression, like so:
$text = preg_split('/( |,|&)/', $text);
I'd go with strtok(), eg
$delimiter = ' &,';
$token = strtok($sms['sms_text'], $delimiter);
while ($token !== false) {
echo $token . "\n";
$token = strtok($delimiter);
}