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.
Related
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(.*)
This question already has answers here:
What does the $1$2$4 mean in this preg_replace?
(3 answers)
Closed 4 years ago.
I want to loop through an array converting specific key/value pairs that contain markup to HTML.
So an example value for $comment['comment_text'] would be:
This has *bolded* text
And should become:
This has <strong>bolded</strong> text
Here's what I've tried:
$pattern = "/\*\b.*?\b\*/i";
$newComment = preg_replace($pattern, "<strong>$&</strong>",
$comment['comment_text']);
And what I get:
This has $& text
I realize I'm mashing up Javascript with PHP, but reading about back references in PHP hasn't made things any clearer.
My strings may have multiple bolded (in markup) instances...
Any help appreciated.
UPDATE:
Apologies - I didn't realize that Stackoverflow was converting asterisks to italics. I converted the example to code.
Also, my confusion came down to the use of $0 vs. $1. Which I still don't fully understand. I thought the numbers referred to the matches in the string...so if you had 5 instances you could refer to them by $0 through $4.
If you use $0 you get:
This has <strong>*bolded*</strong> text
But if you use $1 you get the desired result.
Do this.
$pattern = "/\*\b(.*?)\b\*/";
$newComment = preg_replace($pattern, "<strong>$1</strong>", $comment['comment_text']);
Here $1 refers to the group 1 match. Here I'm supposing that you want to make text between ** bolded.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I am using the following regex
'/\#(.*)\((.*)\)/'
And I am trying to get #ONE(TWO) one and two from the expression. Which works as long as it's the only time that it can be found before an end of line (I think)
I am quite green with regex and I really cannot understand what I am doing wrong.
What I need is to be able to get all the ONE/TWO couples. Can you please help me.
I am working with PHP and the following function
$parsed_string = preg_replace_callback(
// Placeholder for not previously created article
// Pattern example: #George Ioannidis(person)
'/\#(.*)\((.*)\)/',
function ($matches) {
return $this->parsePlaceholders( $matches );
},
$string
);
The results I am getting from https://regexr.com
* expression is greedy by default. For example such regexp (.*)a will return you bdeabde result on bdeabdea string. You should use special ? symbol for non-greedy * behavior. In your case try to use /\#(.*?)\((.*?)\)/ regexp.
This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 1 year ago.
I have been really trying to do some pretty basic work in PHP as I am beginner to PHP but I wasn't really able to achieve my goal.
What I want to do is that I have strings like..!
Mixed Types :
THIS IS A SENTENCE
tHis Is a SEnTenCe
I really tried with ucwords function in php but that didn't gave the exact result as ucwords focus just on first alphabets of every word and do not focus on other remaining characters in word..What I want really is to get some sort of this fixed type of strings from each and every mixed type of strings..!
Like :
This Is A Sentence
Or May be I didn't check it properly..So if anyone can guide me in the right path.That would be great.!
Convert to lowercase first:
$string = ucwords(strtolower($string));
<?php
$l = 'tHis Is a SEnTenCe';
echo ucwords(strtolower($l));
?>
Result
This Is A Sentence
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.