PHP regex on mention (#name) [duplicate] - php

This question already has answers here:
Finding #mentions in string
(6 answers)
Closed 5 years ago.
I have busting my balls on this one, and I have been trying numerous regex'es but just can't seem to get it right. (I am not that experienced in regex).
The following situation is going on, lets take this basic sentence for example;
I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?
The trick here is to get only the #john-doe and #foo-bar parts from the string, preferably in an array:
$arr = [
'#john-doe',
'#foo-bar'
];
Could someone help me get on the right track?

You can use this regex (\#(?P<name>[a-zA-Z\-\_]+)) :
<?php
$matches = [];
$text = "I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?";
preg_match_all ("(\#(?P<names>[a-zA-Z\-\_]+))" ,$text, $matches);
var_dump($matches['names']);
In this example, I used the ?P<names> to name the capture groups, it's easier to get it.
I've made a Regex101 for you, and a PHP sandbox for test
https://regex101.com/r/ZFWvCG/1
http://sandbox.onlinephpfunctions.com/code/1d04ce64a2a290994bf0effd7cf8f0039f20277b

Related

How to put specific string in into array? [duplicate]

This question already has answers here:
PHP: Best way to extract text within parenthesis?
(8 answers)
Closed 3 years ago.
I have a string like this:
[Rise] and rise [again] until lambs become [lions].
I want to get all the string inside the [] (Rise, again, lions) and put it in to an array. How can I do this?
Edit: Thank you guys, I already found the solution here: PHP: Best way to extract text within parenthesis?
You can do it with regular expressions:
$s = '[Rise] and rise [again] until lambs become [lions].';
preg_match_all('/\[([^\]]+)\]/', $s, $m);
print_r($m[1]);

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

Find string, then find string in-between chars [duplicate]

This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
I'm having a hard time with this....
So, I have a load of text:
a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";
Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.
I was trying
$betfred_show = 'betfair_affiliate_id";s:3:"';
$betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);
which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.
Any suggestions?
It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];

"Function eregi() is deprecated" tried preg_match - no bueno [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 4 years ago.
I've read many threads here about this being deprecated and tried replacing it with preg_match but I don't know php enough to fix the rest of the line.
Been enjoying Fotoholder for years, I would switch to a newer similar single file gallery code but then I would lose all my descriptions in the gallery.
Please help resurrect Fotopholder!
( https://github.com/offsky/Fotopholder )
Here are the 2 parts that have eregi:
if(substr($entry,0,1)!="." && !preg_match("#_cache#i",$entry) && is_dir($path."/".$entry)) {
and the 2nd eregi:
if(substr($entry,0,1)!="." && !eregi("_cache",$entry)) {
Thank you very much for your help.
Deprecated means, that the function eregi() is likely to get deleted from the language.
Please use preg_match().
While you still can use eregi(), at a certain point of time your application might not be able to execute any more.
That said: Your code posted is far to big to get a detailed answer.

Moving from preg_replace to preg_replace_callback [duplicate]

This question already has answers here:
Replace preg_replace() to preg_replace_callback() [duplicate]
(2 answers)
Closed 5 years ago.
I've had a good look through all the previous topics and I don't understand enough PHP to use them to answer my questions so sorry in advance if this is really simple!
{
$content = preg_replace('/\$([\w]+)/e', '$0', $this->getTemplateStyle());
$custom_css = $this->getCustomCSS();
return $content.$custom_css;
}
And I need to replace preg_replace with preg_replace_callback. I know it's not a simple switch and that I need to add more to the code, but I don't know what to add. Thanks in advance for your help.
[SOLVED]
You seem to be trying to inject variables from the current scope into your string. I'll leave aside why this is a bad idea and assume there is no user input involved. First get the current scope:
$scope = get_defined_vars();
Next use the callback:
preg_replace_callback('/\$(\w+)/',function($m) use ($scope) {if( isset($scope[$m[1]])) return $scope[$m[1]]; else return $m[0];}, $this->getTemplateStyle());
Job done. – #Niet the Dark Absol

Categories