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" '>;
Related
How to convert this string
*|text:student:required|*
( * and | is part of string ) into array like this
['text' ,'student','required']
try the following:
$str = '*|text:student:required|*';
$str = preg_replace("/[|*]/", '', $str);
$arr = explode(':', $str);
this simply removes the | AND * from the string using preg_replace() and the turns the string into an array using explode
Here you go:
$str = "|text:student:required|";
$str = trim($str,"|");
$str = trim($str,"*");
$x = explode(':',$str);
print_r($x);die;
The shortest one with preg_split function:
$s = '*|text:student:required|* ';
$result = preg_split('/[*:| ]+/', $s, -1, PREG_SPLIT_NO_EMPTY);
print_r($result);
The output:
Array
(
[0] => text
[1] => student
[2] => required
)
$string = "*|text:student:required|";
$string = str_replace("*", "", str_replace("|","", $string));
$array = explode(':', $string);
I have an string
$str = 'one,,two,three,,,,,four';
I want to array like this(output of print_r)
Array ( [0] => one,two,three,four )
My code is
$str = 'one,,two,three,,,,,four';
$str_array = explode(',', $str);
print_r($str_array);
But not working Because multiple commas side by side.How can i solve this problem?
You can used array_filter function to removed empty element from array.
So your code should be:
$str = 'one,,two,three,,,,,four';
$str_array = array_filter(explode(',', $str));
print_r($str_array);
Edited Code
$str = 'one,,two,three,,,,,four';
$str_array = implode(',',array_filter(explode(',', $str)));
echo $str_array; // you will get one,two,three,four
You can remove multiple comma using preg_replace as
$str = 'one,,two,three,,,,,four';
echo $str_new = preg_replace('/,+/', ',', $str);// one,two,three,four
$str_array = explode(' ', $str_new);
print_r($str_array);//Array ( [0] => one,two,three,four )
try this
<?php
$string = 'one,,two,three,,,,,four';
$new_array = array_filter(explode(',', $string));
$final_array[] = implode(',',$new_array);
print_r($final_array);
?>
OUTPUT: Array ( [0] => one,two,three,four )
<?php
$string = 'one,,two,three,,,,,four';
$result = array(preg_replace('#,+#', ',', $string));
print_r($result);
Output:
Array ( [0] => one,two,three,four )
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
I have a text file with a lot of strings now i want to put those strings into a array.
I need to set between the screen quites "",
Text file is saved as,
String1
String2
String3
This most be
"String1", "String2, "String3"
Wat is the simplest way to do this in php/html/jquery ?
Try this:
Use file() to create an array from the lines of the text file, and array_map() to add the quotes:
$lines = file('myfile.txt');
$lines = array_map(function($x){return $x = '"'.$x.'"';}, $lines);
See this demo
in php simply use file() function
<?php
$array = file('file.txt');
http://php.net//manual/en/function.file.php
To get your array you'd use this:
$lines = explode("\n", $string);
Which returns:
Array
(
[0] => String1
[1] => String2
[2] => String3
)
EXAMPLE
Now the full example would look like this:
$lines = explode("\n", $string);
$a = array();
foreach($lines as $line) {
$a[] = "\"". trim($line) ."\"";
}
$string = implode(', ', $a);
print_r($a);
echo "String: " . $string;
That gives you the output of:
Array
(
[0] => "String1"
[1] => "String2"
[2] => "String3"
)
String: "String1", "String2", "String3"
EXAMPLE
This question already has answers here:
Creating an array from a string separated by spaces
(7 answers)
Closed 1 year ago.
how to convert a string in array in php i.e
$str="this is string";
should be like this
arr[0]=this
arr[1]=is
arr[2]=string
The str_split($str, 3); splits the string in 3 character word but I need to convert the string after whitespace in an array.
Use explode function
$array = explode(' ', $string);
The first argument is delimiter
With explode function of php
$array=explode(" ",$str);
This is a quick example for you http://codepad.org/Pbg4n76i
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
try json_decode like so
<?php
$var = '["SupplierInvoiceReconciliation"]';
$var = json_decode($var, TRUE);
print_r($var);
?>
Take a look at the explode function.
<?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
There is a function in PHP specifically designed for that purpose, str_word_count(). By default it does not take into account the numbers and multibyte characters, but they can be added as a list of additional characters in the charlist parameter. Charlist parameter also accepts a range of characters as in the example.
One benefit of this function over explode() is that the punctuation marks, spaces and new lines are avoided.
$str = "1st example:
Alte Füchse gehen schwer in die Falle. ";
print_r( str_word_count( $str, 1, '1..9ü' ) );
/* output:
Array
(
[0] => 1st
[1] => example
[2] => Alte
[3] => Füchse
[4] => gehen
[5] => schwer
[6] => in
[7] => die
[8] => Falle
)
*/
explode() might be the function you are looking for
$array = explode(' ',$str);
explode — Split a string by a string
Syntax :
array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
$delimiter : based on which you want to split string
$string. : The string you want to split
Example :
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
In your example :
$str = "this is string";
$array = explode(' ', $str);
You can achieve this even without using explode and implode function.
Here is the example:
Input:
This is my world
Code:
$part1 = "This is my world";
$part2 = str_word_count($part1, 1);
Output:
Array( [0] => 'This', [1] => 'is', [2] => 'my', [3] => 'world');
here, Use explode() function to convert string into array, by a string
click here to know more about explode()
$str = "this is string";
$delimiter = ' '; // use any string / character by which, need to split string into Array
$resultArr = explode($delimiter, $str);
var_dump($resultArr);
Output :
Array
(
[0] => "this",
[1] => "is",
[2] => "string "
)
it is same as the requirements:
arr[0]="this";
arr[1]="is";
arr[2]="string";