1

According to PHP manual "If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."

How can I return a value from a string with only knowing the first few characters?

The string is dynamic and will always change whats inside, but the first four character will always be the same.

For example how could I return "Car" from this string "TmpsCar". The string will always have "Tmps" followed by something else.

From what I understand I can return using something like this

preg_match('/(Tmps+)/', $fieldName, $matches);

echo($matches[1]);

Should return "Car".

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
LukeDS
  • 141
  • 8

5 Answers5

4

Your regex is flawed. Use this:

preg_match('/^Tmps(.+)$/', $fieldName, $matches);
echo($matches[1]);
The Coprolal
  • 896
  • 8
  • 8
  • Sorry but could i ask what does (.+)$ do? – LukeDS Dec 17 '18 at 15:03
  • 2
    @LukeDS The dot represents 'any character except linebreaks'. The plus represents 'matching one or more of the preceding token'. So in human language the (.+) means match anything after 'Tmps'. Furthermore its wrapped in brackets so its a capture group which means you can easily acces the captured value as shown in the answer. (The $ is just an end of string character) – Arjan Dec 17 '18 at 15:10
3
$matches = []; // Initialize the matches array first
if (preg_match('/^Tmps(.+)/', $fieldName, $matches)) {
    // if the regex matched the input string, echo the first captured group
    echo($matches[1]);
}

Note that this task could easily be accomplished without regex at all (with better performance): See startsWith() and endsWith() functions in PHP.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
3

"The string will always have "Tmps" followed by something else."

You don't need a regular expression, in that case.

$result = substr($fieldName, 4);

If the first four characters are always the same, just take the portion of the string after that.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

An alternative way is using the explode function

$fieldName= "TmpsCar";
$matches = explode("Tmps", $fieldName);
if(isset($matches[1])){
    echo $matches[1]; // return "Car"
}
executable
  • 3,365
  • 6
  • 24
  • 52
-1

Given that the text you are looking in, contains more than just a string, starting with Tmps, you might look for the \w+ pattern, which matches any "word" char.

This would result in such an regular expression:

/Tmps(\w+)/

and altogether in php

$text = "This TmpsCars is a test";
if (preg_match('/Tmps(\w+)/', $text, $m)) {
    echo "Found:" . $m[1]; // this would return Cars
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
  • This is not what was asked. The question states that "the first four character will always be the same", and it makes no statements about the kind of characters that follow. So the sufficient regex would be `/^Tmps(.+)$/` and the check for success of preg_match would be obsolete – The Coprolal Dec 18 '18 at 16:23
  • @TheCoprolal Given the first 4 chars are always Tmps, and he want the rest of the string, the whole regex requirement is obsolete, as he can just use `substr($text, 4)`. And btw. my answer is works for what he asked. As the first four chars are always Tmps, you don't need need the `^`-anchor and you also don't need the `$` as `+` is gready. You could just complain about the char class `\w` - but that wasn't specified by the author – Philipp Dec 18 '18 at 16:42
  • sure, but this way the OP learned something about regexes too :-) I upvoted the `substr` solution btw – The Coprolal Dec 19 '18 at 10:51