Find the last hyperlink in a string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to find the last hyperlink in a string. The hyperlink may begin with one of the following:
http://
https://
market://
Is there a regex method in PHP to find this?
Thank you.

You can use this negative lookahead based regex:
~\b(?:https?|market)://\S+?(?!.*?\b(?:https?|market)://)~i

$regex = "/(http|https|market)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "http://link.com some text in between https://link.com more text in between market://link.com";
if(preg_match_all($regex, $text, $url))
{
echo $url[0][count($url[0])-1]; // Outputs: market://link.com
}

Related

REGEX for alternate characters in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string mystring. I have to replace the alternate characters with x so that final output is mxsxrxnx. All the alternate characters starting from 2nd position are replaced with x. I can do it with loop but is there a Regular expression for that or a better way in PHP? Please help.
Using a reset match meta-character \K you are able to do it:
.\K.
Live demo
PHP:
echo preg_replace('/.\K./', 'x', 'mystring'); // mxsxrxnx

Prepend and append to each variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
First question here!
I have some PHP variables all in the form of the below, in around 7/8 different files.
$this->do['string']
I wish to make them all into the following:
myfunction($this->do['string'])
I understand I have to use regex and the get file contents function, but I'm unsure where to go from here! Would be very grateful if someone can assist!
Thanks in advance
How about:
$result = preg_replace("/(\$this->do\['[^']+'\])/", "myfunction($1)", $inputstring);
Edit according to comment:
In the comment you said that your input string is: $inputstring = "\$this->do['fsdfs']"; (with a backslash before $this). You have then to add the backslash in the regex and also escape it, so the whole instruction becomes:
$result = preg_replace("/(\\\$this->do\['[^']+'\])/", "myfunction($1)", $inputstring);
// ^^^

preg_match pattern with signal character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need a pattern which is able to find a word wrapped in two signal characters.
Basically something like
$string = "bablabla __test__ blablabla"
preg_match("/__\w__/", $string, $result);
print_r($result);
\w is a single word character. It would match __t__ but not multiple characters like __test__. Try \w+

Finding a string and replacing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to find MUSECMS_WIDGET(NAMEOFWIDGETHERE); and get the NAMEOFWIDGETHERE? And then replace the MUSECMS_WIDGET(NAMEOFWIDGETHERE); with
<!--NAMEOFWIDGETHERE--!>?
Please consider:
http://php.net/manual/en/function.preg-replace.php
If you don't know about regular expressions please read up on them here:
http://www.zytrax.com/tech/web/regex.htm
Here's a simple example:
$var = str_replace("NAMEOFWIDGETHERE", "Your Replacement", $subject);
The $subject is your original String.

Regex Replace pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need the regular expression to replace using following rule:
Any integer present before . then no take place.
If no integer present before . then that should be replace.
$input = (contain anything characters, symbols, numbers, floats etc)
Example:
$myString = "Example 1.58 Stack.68";
Output should be
Example 1.58 Stack,68
preg_replace('#(?<!\d)\.#',',',$myString)

Categories