Regex PHP part of string [duplicate] - php

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?

There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

Related

PHP Trouble replacing string containing dash and underscore [duplicate]

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 6 years ago.
I have tried, read and searched to to avail.
I just need to replace -m_ with -b_ in a string containing an image name / location.
For instance:
I just want to replace;
some-image-name-hyphenated-m_15235101.jpg
with
some-image-name-hyphenated-b_15235101.jpg
Easy right?
$mediumimage = 'some-image-name-hyphenated-b_15235101.jpg';
I tried
$biggerimage = preg_replace("-m_", "-b_", $mediumimage);
and
$biggerimage = preg_replace('-m_', '-b_', $mediumimage);
Also backslashing along with other tries resulting from searches.
Warning: preg_replace(): No ending delimiter '-' found
I don't feel well right now...
I beseech anyone who is smarter than me e.g..... any one here.
Try use $biggerimage = str_replace("-b_", "-m_", $mediumimage);

PHP isolate character surrounded by special character [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
I have the following string:
$db_string = '/var/www/html/1_wlan_probes.db';
I want to isolate/strip the number character so that I only have the following left:
$db_string = '1';
So far I havn't found an simply solution since the number that needs to be found is random and could be any positive number. I have tried strstr, substr and custom functions but none produce what I am looking after, or I'm simply overlooking somehthing really simple.
Thanks in advance
You should use the preg_match() function:
$db_string = '/var/www/html/1_wlan_probes.db';
preg_match('/html\/(\d+)/', $db_string, $matches);
print_r($matches[1]); // 1
html\/(\d+) - capture all the numbers that come right after the html/
You can test it out Here. It does not matter how long the number is, you're using a regular expression to match all of them.

Regex in PHP: How to get word in last part URL? [duplicate]

This question already has answers here:
Regex in Notepad++ : How to get last part of URLs?
(4 answers)
Closed 8 years ago.
I have some lines like this:
/gtrhr/5435-silent-library
/fghf3/435-silent-librar
/gfgr2gf/8768-silent-lib
How to get ONLY words in last part of URL ?
So, the oupout will be:
silent-library
silent-librar
silent-lib
Thank you !
[A-Za-z][A-Za-z\-]+$
The $ enforces the match ends with the end of the line.
See here on Regexr.
You could also go for a more general [a-z][\w\-]+$ but that depends on precisely which characters are valid or not in your inputs.

Php : regular expression to find a text between strings [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx How to find text between two strings
I am new to REGEX and learning..this is emergency and some one need to help me,
how can i get a value 6Lf4 (dynamic value) ,
private="key" value="6Lf4" sent="yut"
P.s there are lots of attribitute named "Value" in an string, so i need some regex to find string between ="key" value=" , and "
This should work given a strict format:
$matches = array();
preg_match_all('/="key" value="([^"]*)"/', $inputString, $matches);
This tests specifically for the given format with no exceptions. If there is a variable amount of whitespace between "key" and value, you can change the regex to have key"\s+value instead.

Is this regex right (eregi to preg_match conversion) [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I am replacing PHP deprecated functions in my website. I have this code:
(eregi("<[^>]*object.*\"?[^>]*>", $secvalue))
It is written on the php.net site that it eregi should be replaced with preg_match with i modifier.
Is this coded right?
(preg_match("<[^>]*object.*\"?[^>]*/i>", $secvalue))
or should I place /i somewhere else?
preg_match('/<[^>]*object.*\"?[^>]*>/i', $secvalue)
You need to add a forward slash at the beginning to match the closing one:
preg_match('/<[^>]object."?[^>]*>/i', $secvalue);

Categories