Preg Split not working if use variable in pattern - php

I have string like this and i want to split with matched pattern
$string = '[(123,234,34)tes2t.jpg*#*test.jpg]';//dynamic no of files
so if i try like this,
$keywords =preg_split( "/(?<=test.jpg)/", $string,'-1');
it is working but i need to pass variable in pattern
$file = 'test.jpg';
$keywords =preg_split( "/(?<=$file)/", $string,'-1');
This is not working.please help.
Thanks in advance.

$file = 'test.jpg';
$keywords =preg_split( '/(?<='.$file.')/', $string,'-1');

Related

How to remove only links from string and nothing more

I have string: Some text (some text) anylink.com www.anylink.com http://anylink.com R.I.O one-www.link.com
I have this code:
$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "";
$name = preg_replace($pattern, $replacement, $name);
It removes fine: anylink.com, www.anylink.com, http://anylink.com
But its also remove word R.I.O and one- how I can avoid this?
Thanks for help.
I believe that you should be sure of what is really a link or not
you need to find a pattern
I realize that all your links may end with .com
so try this regex "[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+.com$"
but if you need .com.anything or .com/anything
"[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+.com.*$"

how to get filename based on the number of characters after the dot

hello here is what I have :
filename mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw
using php how do I capture the name of the file which would be anything.ext and get rid of the rest.
so return : mycoolfile.zip
I know it can be accomplished using regular expressions but I am not familiar with those, I am open to any suggestions or any way to accomplishing the task as long as it works for anyfilename.ext?blablablaafter
it doesnt matter if its regex or not as long as it works. any help please
$path = '/lul/what/mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$file = basename(preg_replace('/\?.*$/', '', $path));
Use parse_url to get the name
<?php
$str = "mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1";
$parsed_url = parse_url($str);
echo $parsed_url["path"] . "<br>";
You can make use of parse_url function:
$u = 'filename mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$arr = parse_url($u);
echo $arr['path']; //=> OUTPUT: filename mycoolfile.zip
Could you use '?' as a deliminator in explode() and use the value from an array, or perhaps use preg_split()?
edit: or use the much better solutions above! :-)
You may also try this
$u = 'mycoolfile.zip?dl=1&token_hash=AAHy4rl_jpJF4Z70W1BQUgXf7IOvLXw';
$url=explode("?",$u);
echo $url['0'];

PHP how do i cut substring after last slash?

Hi i want to know how can i get substring from string after last slash?
In short i want to get the file name from path.
for example i got string like this:
test-e2e4/test-e2e4/test-e2e4/6.png
and i want to get 6.png how can i do that ?
I got dir only and the file name can be all format, it can be also something else then file
Or test-e2e4/test-e2e4/test-e2e4/aaaaa and want to get aaaaa
Regex maybe? Or maybe you know some nice functions which will do it for me ?
In addition to other replies, there's actually a function in PHP to do this: basename. Example:
$string = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$base = basename($string); // $base == '6.png';
$string = 'test-e2e4/test-e2e4/test-e2e4/aaaaa';
$base = basename($string); // $base == 'aaaaa'
$string = '6.png';
$base = basename($string); // $base == '6.png'
Full details here: http://php.net/basename
Do like this..
$yourstring = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$val = array_pop(explode('/',$yourstring)); // 6.png
You can try explode and array_pop functions to work this out:
$str = 'test-e2e4/test-e2e4/test-e2e4/aaaaa.png';
$str = explode('/', $str);
$filename = array_pop($str);
echo $filename; //Output will be aaaaa.png
...Or you can use the following regex:
[^\/]*$
You don't need to use regex for this, you can use substr() to get a portion of the string, and strrpos() to specify which portion:
$full_path = "test-e2e4/test-e2e4/test-e2e4/6.png"
$file = substr( $full_path, strrpos( $full_path, "/" ) + 1 );
substr() returns a portion of the string, strrpos() tells it to start from the position of the last slash in the string, and the +1 excludes the slash from the return value.

PHP regular expression for directory

I need a regular expression that would take the string after the last forward slash.
For example, considering I have the following string:
C:/dir/file.txt
I need to take only the file.txt part (string).
Thank you :)
You don't need a regex.
$string = "C:/dir/file.txt";
$filetemp = explode("/",$string);
$file = end($filetemp);
Edited because I remember the latest PHP spitting errors out about chaining these types of functions.
If your strings are always paths you should consider the basename() function.
Example:
$string = 'C:/dir/file.txt';
$file = basename($string);
Otherwise, the other answers are great!
The strrpos() function finds the last occurrence of a string. You can use it to figure out where the file name starts.
$path = 'C:/dir/file.txt';
$pos = strrpos($path, '/');
$file = substr($path, $pos + 1);
echo $file;

How to divide string in php

How can I add divider ; in the following variable which contains string
I have string like this:
$filename = "a.jpg3c.pngyes.jpg";
I would like to have something like
a.jpg;3c.png;yes.jpg
This string is created when I select multiple files to upload.
Is regex only solution in here?
Regex is not the only solution! Perhaps you can use str_replace() instead of regex.
$filenames = "a.jpg3c.pngyes.jpg";
$img_extensions = array(".png", ".jpg", ".gif");
$semicolon_additions = array(".png;", ".jpg;", ".gif;");
$newfilenames = str_replace($img_extensions, $semicolon_additions, $filenames);
http://php.net/manual/en/function.str-replace.php
Edit: In your particular case, I would add in the semicolon at the end of the filename inside of your loop.
Here is one option using regular expressions:
$filename = "a.jpg3c.pngyes.jpg";
$regex = '/\.(jpg|png|gif)(?!$)/';
$filename = preg_replace($regex, ".$1;", $filename);

Categories