I am using a regular expression to convert #user name to links.
For example if user enters #Alex Ferguson it should convert Alex Ferguson to hyperlink.
Here it's converting the first name to hyper link and excluding the last name.It looks for the word closer to #, if there is no space between first name and last name it works fine.
Is there any way to convert both first name and last name to hyper link.
Here is my code:
function convert($msg){
$message = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('$1', '$1#$2', '$1#$2'), $msg);
return $message;
}
Thanks..
The general method for this would be:
$regex = '~(?i)#[a-z]+[ ][a-z]+~';
$replaced = preg_replace($regex,'$0',$string);
Notes
I'll leave it for you to fill in the blanks
One issue with names is the range of allowable characters. What about Julie O'Hara? M.C. Cocoa? etc.
Related
I have a system where users can nput customer information. When the information is enetered I do a few things to clean the information such as changing the case, removing special characters etc. The one issue I have though is that Limited companies have the following syntax, company name:
company name SL
As I currently change everything to lower case and then use ucwords I end up with Sl. I am looking for the best way to overcome this and regex sprang to mind.
Unfortunately regex is not my strong point and was hoping someone could point me in the right direction. What I am hoping to do is to find the a string that contains two letters S and L in this order. I need to be able to find the string regardless of characters ie S.L., S.L and also regardless of case. If the string is found, replace this with SL.
Within this I would also need to know the characters it found to use string replace to change it.
If you imagine my current method using string replace is growing quite big:
return str_replace(array(',','sl.','s.l.','s.l','sl ',' sl','SL.','Sl.','S.L.','S.l.','S.l','S.L','SL'),array("","SL","SL","SL","SL","SL","SL","SL","SL","SL","SL","SL","SL"),self::properCase($name))
The issue with the above is also, if some enters say "Bill Slade sl", without a regex to match only those two letters, how could I ever say only upper case them. I need to ensure there is nothing either side.
Any help or pointers would be greatly appreciated.
Use this RegEx:
/\bs\.?l?\.?\b/i
RegEx Demo and Explanation
Using this RegEx with PHP:
$regex_pattern = "/\bs\.?l?\.?\b/i";
$string = "company name S.l\ncompany name Sl.\ncompany name S.l.\ncompany name Sl\ncompany name s.l.\ncompany name sl\ncompany name s.L";
$replacement = " SL";
$result = preg_replace($regex_pattern, $replacement, $string);
echo $result;
Try this working code on http://writecodeonline.com/php/ so you can see the results quickly. :)
Hope it helps.
Read up: preg_replace | PHP manual
You can use this regex: \bs[.]?l(?:[.]?|\b) and substitute with SL.
Here is an example and sample working code on TutorialsPoint:
$re = "/\\bs[.]?l(?:[.]?|\\b)/i";
$str = "company name Sl\ncompany name s.l.\ncompany name sl\ncompany name s.L";
$subst = "SL";
$result = preg_replace($re, $subst, $str);
$replacement= "SL";
$pattern = '/(\bs)(.?)(\s?)(l\b)(.?)/i';
$input = "
company name Sl
company name s.l.
company name sl
company name s.L
company name s. L
company name s l
company name S, L.
";
$result = preg_replace($pattern, $replacement, $input);
echo $result;
Go to this http://www.phpliveregex.com/p/aJi link and click to preg_replace
I'm currently looking to implement a search function on my website.
I have it working with 1 word/name, but I can't seem to figure out how to split and identify certain parts of the search string.
Example:
I have a user in my database with the name "Steve de Vette"
(My country has words in between almost all of the first and last names but not always, and sometimes more than one. ex: "Kees van der Berg") But his name is of course split up in multiple parts. "vNaam", "Tvoegsel"(meaning the "de" or "van der") and "aNaam".
This complicates things a bit for me, since I now have to split the search string, which on it's own isn't a big deal. But I need to know how I can get the correct results every time.
So I guess it comes down to this: How can I make it so that the name is split up like it should, or maybe there's a way to strip these thing all together, but for the likes of me I can't seem to figure it out.
Any help would be greatly appreciated!
EDIT:
I have tried just exploding the name and searching with multiple OR_LIKE clauses. This works until I have no "tussenvoegsel" and one of the Like statements reads "OR anaam LIKE '%%'"
split the string with explode and search for the first and last item.
$string1 = "Steve de Vette";
$string2 = "Kees van der Berg";
$ex1 = explode(" ", $string2);
$nr = count($ex1);
echo $ex1[0]; //firstname
echo ' ';
echo $ex1[$nr-1]; //lastname
Well you can use the PHP string searching funciton.
$pos = strpos($string, $character);
You could use this to find the first space in the name. So if you take "Steve de Vette", you could first find Steve as the first name, then the rest of the string you could search again or keep the rest of it as the last name.
This is a snippet of code taken from my own site.
$fname = strstr($entry," ",true); <-- finds the first name (all characters up to the first space)
$len = strlen($fname) + 1; <-- skips over the space to the last name
$entrylen = strlen($entry); <-- gets the length of the search string used
$sname = substr($entry, $len, $entrylen); <-- gets the rest of the string (last name)
Hope you find this helpful
What i do is strip out any spaces all together. I store spaces in my database like normal but use the replace feature when searching to strip out spaces. then strip out spaces from the search field as well and use the like with the wild card on the right hand side. I try to make the search as simple as possible. searching with one word seems to work better all together so forcing one word seems to be the thing that works for me.
I have long text being extracted using file_get_contents(). The text file contains information in the following format:
---
Description:
---
Some description here, with long text sentences.
---
Part 1
---
Information with part 1 in this section followed by path 2.
Now i wish to style the information between ---, for example i would like to make "description" and "Part 1" bold and display the rest in plain text.
I think that can be achieved by preg_match. But i would like to know if any other method can be used too.
The following should work:
preg_replace('/---(.*?)---/s', '<strong>$1</strong>', $text);
The expression captures anything between ---- pairs. $1 in the replacement pattern indicates a backreference - it contains what was matched by the first capturing group. The s modifier makes . also match the newlines.
If you'd like to also remove the whitespace, you could do this:
preg_replace('/---\s*(.*?)\s*---/s', '<strong>$1</strong>', $text);
If there's a possibility that --- pairs occur inside the text, then you can use the following pattern instead:
preg_replace('/---(?=\s)(\s)([^\r\n]+)(\s)---/s','<strong>$2</strong>$3', $text);
Regex101 Demo
You could use also explode
$expl = explode("---",$yourtext);
echo '<b>'.$expl[0].'</b>'; //**Description:**
echo $expl[1]; //Some description here, with long text sentences.
echo '<b>'.$expl[2].'</b>'; //**Part 1**
echo $expl[3]; //Information with part 1 in this section followed by path 2.
You can use a regular expression to do this. The following will work even if you have hyphens in the text you want bolded.
echo preg_replace('/---(\r\n|\n|\r)([^\n\r]+)(\r\n|\n|\r)---/s', '<strong>$2</strong>$3', $text);
For example, suppose your text is:
---
Descrip---tion:
---
Some description here, with long text sentences.
---
Part 1
---
Information with part 1 in this section followed by path 2.
The above code would replace this with:
<strong>Descrip---tion:</strong>
Some description here, with long text sentences.
<strong>Part 1</strong>
Information with part 1 in this section followed by path 2.
I want to extract values from string. here is a sample string
"Sample string Name: Mosa Phone: 020202020 Email: email#domain.com the rest of the sample string"
The phone doesn't necessary to be sequence of numbers it could be like (00-98550-22) or (+025-588) or (92122/222)
The good news is that these fields are always consecutive either separated by tab, white space, or new line.
so I am thinking how can I make it find the fields until the next field is found, so we can say find Name: then continue until you find Phone:
I am trying to achieve this using regex
This is the code I already wrote, but each field is evaluated alone
$namepattern = "/(Name\:\s[a-zA-Z]+\s[a-zA-Z]+)/";
$phonepattern = "/(Phone\:\s\d+)/";
$emailpattern = "/(Email:\s([_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})))/";
I'm using this regex to match name, phone and email in one:
/Name:\s(.+)\sPhone:\s(\d+)\sEmail:\s([a-zA-Z0-9_-]+#[a-zA-Z0-9]+.[a-zA-Z]+)/
Here's a demo: http://regex101.com/r/uO4tF7
This will match the needed
/Name:\s(.+)\s*Phone:\s(.*)\s*Email:\s([a-zA-Z0-9_.-]+#[a-zA-Z0-9]+.[a-zA-Z]+)/
My linking structure for user input:
++visible part of link====invisible HTML address part of link++
Input string:
some text here some text here ++stack overflow====http://stackoverflow.com/questions/ask++ some text here ++examplesite.com====http://www.examplesite.com/article?id=1++ some text here some text here some text here some text here ++shouldnotmatch.com====http://w ww.shouldnotmatch.com/++ some text here.
My aim:
If the part between ==== and ++ includes one or more space character(s), preg_match_all should not match. So my desired output is to match with first two linking attempts. But the last linking attempt should not match since w ww includes one space character.
My unsuccessful attempts:
\+\+(.+?)====(.+?[^ ])\+\+
\+\+(.+?)====(.+?[^ {1, }])\+\+
Can you please correct me?
With your first attempt you were allowing all characters before the space verification.
Does something like this work?
!\+\+(.+?)====([^ ]+?)\+\+!
If there is always something between those parenthesis then you can drop the ?
!\+\+(.+?)====([^ ]+)\+\+!
Try this regular expression :
[+]{2}(.+?)[=]{4}([^\s]+?)[+]{2}