I have a string that is:
<p><img src="../filemanager/image.png?1476187745382"/></p> some text ...
I would like to remove everything after a .png or .jpg when question mark occurs. The goal is to remove the timestamp added ?1476187745382 but not the "/></p> some text ...
Keeping in mind that the timestamp, will change and the what comes after the the image > will also be different.
I have looked at different solutions, but they all remove either the exact occurrence or everything after a certain character, which is not what I need to do.
This is what I have looked at:
PHP remove characters after last occurrence of a character in a string
Remove portion of a string after a certain character
Can someone point me to the right direction?
Not always needed, but a regex will do it:
$string = preg_replace('/\?[\d]{13}/', '', $string);
If the timestamp is not always 13 digits then replace the {13} with just a +.
$path = "../filemanager/image.png?1476187745382";
$subpath = explode('?',$path)[0];
Related
Im having problem with changing multiple HEX colors into span. Current code just change one color. Any idea how to make it work for multiple colors ?
function convertHexToSpan($name)
{
$name = preg_replace('/\*#([a-f\d]{6})(.*)\*[a-f\d]+/', "<span style='color:$1'>$2</span>", $name);
return $name;
}
$text = "#ff6600Hello #ff0000world";
$newText = convertHexToSpan($text);
OUTPUT SHOULD BE "<span style='color:#ff600'>Hello</span><span style='color:#ff0000'>world</span>
Updating your Regular Expression will get you most of the way there, but we have to make some assumptions that differ slightly from your original question.
If you use the following as the expression:
/(#[a-f\d]{6})([^ ]+)/
preg_replace does the repetition searching for you as regex isn't really for iterating, so I removed the second hex search. This finds the 6 hex digits as a first group, then the next group is any character that is not a space.
Note: I am assuming that you are trying to break on word boundaries, but will need to modify if that is not the case. I am also assuming you want to preserve the space between the words after conversion, but your example shows no space.
To remove the space between words, you would just need to modify the regex to match the spaces (and then they will get removed), which would be as follows:
/(#[a-f\d]{6})([^ ]+)( )+/
I'm using the following regexp with PHP's preg_replace:
$f[] = '/href\=\"([a-zA-Z\_]*?).php\?(.*?)\"/';
I want to update this to match all hyperlinks ending in .php with arguments (as it is now), but exclude any links that have the word "phpinfo" in the link..
I've tried:
$f[]='/href\=\"([a-zA-Z\_]*?).php\?(.*?!phpinfo)\"/';
But I fear I am doing it all wrong, it is not working - I have not been able to find a similar example that I am able to adapt to get this working.
Use a negative lookahead based regex.
$f[] = '/\bhref="([a-zA-Z\_]*?).php\?((?:(?!phpinfo|").)*)"/';
DEMO
The trickier part is this (?:(?!phpinfo|").)* which matches any character but not of double quotes or phpinfo, zero or more times. What I mean by "not of phpinfo" is, the following character would be any but not the starting letter in substring phpinfo ie, p. So this would match p only if the following chars must not be hpinfo.
I have the following code to convert links to hyperlinks in a string.
$text_block = preg_replace('$(\s|^)(https?://[a-z0-9_./?=&-]+)(?![^<>]*>)$i', ' $2 ',$text_block);
However, if the link as a period at the end, like "do a search on http://google.com.", the regex includes the period in the link. How can I change the regex above to look for and not include a period if present?
EDIT: For clarification - the $text_block is a large block of text that may contain many links. The regex need to parse through the block of text and find and convert all found links.
EDIT 2: As pointed out below in the comments, I guess you'd have to account for domains like ".co.uk". So I guess it would have to look for and remove the last period that is followed by whitespace, if present... gets tricky. Any ideas?
A not particularly elegant, but purely regex solution is :
$(\s|^)(https?://[a-z0-9_./?=&-]+[a-z0-9_/?=&-])(?![^<>]*>)$i
Just ensures the last character is any of the valid characters except .
PHP
$text_block = preg_replace('$(\s|^)(https?://[a-z0-9_./?=&-]+[a-z0-9_/?=&-])(?![^<>]*>)$i', ' $2 ',$text_block);
Working on RegExr
Try this:
$output = rtrim($string, '.');
I'm trying to pull a word out of an email subject line to use as a category for attached email. Preg_match works great as long as it's not just a single word (which is what I'd like to do anyway). If there is only one word in the subject line, I just get an empty array. I've tried to treat $matches as just a variable in that case, but that doesn't work either. Can anyone tell me if preg_match will work on a single word, or what the better way to do this would be?
Thanks very much
Assuming \b(?:word1|word2|word3)\b
The reason it wont match "word1" is because you included a word separator, the \b.
What you can do is just simply always inject the word separator:
preg_match("\b(?:word1|word2|word3)\b", "." . $subject . ".", $matches);
Crude but effective.
preg_match will work on a string one character long. I think that the issue here is probably your regex. My guess is that you're testing for whitespace and because it isn't finding any it says that there is no match. Try appending '^([^\s]*)$|' to your regex and I wager it will start picking up those one word values. ([^\s] means give me anything which has no spaces in it, | means 'or'. By adding it to the front of your regex, it will include things without whitespace or whatever you already had)
I need to try and strip out lines in a text file that match a pattern something like this:
anything SEARCHTEXT;anything;anything
where SEARCHTEXT will always be a static value and each line ends with a line break. Any chance someone could help with the regext for this please? Or give me some ideas on where to start (been to many years since I looked at regex).
I am planning on using PHP's preg_replace() for this.
Thanks.
This solution removes all lines in $text which contain the sub-string SEARCHTEXT:
$text = preg_replace('/^.*?SEARCHTEXT.*\n?/m', '', $text);
My benchmark tests indicate that this solution is more than 10 times faster than '/\n?.*SEARCHTEXT.*$/m' (and this one correctly handles the case where the first line matches and the second one doesn't).
Use a regex to match the whole line like so:
^.*SEARCHTEXT.*$
preg_replace would be a good option for this.
$str = preg_replace('/\n?.*SEARCHTEXT.*$/m', '', $str);
The \n escape matches the line break for the matched line. This way matched lines are removed and the replace method does not just leave empty lines in the string.
The /m flag makes the caret (^) match the start of each line instead of the start of the string.