I've string like this:
hi my best friend #John How Are You?
I want to extract the name from the string:
john
This string has only one name it can be any number of names the string will have. I want to fetch the string between # and [space].
I've tried using explode() and foreach() loop, but I'm not able to get this value.
$text = "hi my best friend #John How Are You?";
preg_match_all("/(#\w+)/", $text, $matches);
var_dump( $matches );
try this
<?php
$string="hi my best friend #John How Are You?";
preg_match('/(?<=hi my best friend #)\S+/i', $string, $match);
echo $match[0];
Output
Jhon
Try this,
$string = "hi my best friend #John How Are You?";
$arr = explode(" ",$string);
foreach($arr as $val){
if (strpos($val,'#') !== false) {
$result = str_replace("#","",$val);
}
}
echo $result;
The regular expression will do fine but if you still want to do this using explode() and loop you may try the following code :-
$str="Hello my name is #John and this is my friend #julia";
$exp=explode(" ",$str);//exploding string from space
foreach($exp as $key=>$val){
if(strpos($val,"#")===false){continue;
}
else{$new[$k]=str_replace("#","",$val);}
}
print_r($new);
Hope this might help you.
Here is a quick working example
$string = "hi my best friend #John How Are You?";
$explodedstring = explode('#', $string);
$explodedforspace = explode(' ',$explodedstring[1]);
echo $explodedforspace[0];
?>
Related
I have a PHP function which converts #Hashtag into a link...
function convertHashtags($str) {
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', $str);
return($str);
}
It work properly when I use it with a common string
$string = "Hello #World";
$string = convertHashtags($string);
(in this case an output would be: Hello #World
But when I'm trying to insert something from my database to that string it displays, but without that function's effect…
$string = $row["content"];
$string = convertHashtags($string);
(an output: Hello #World)
I am new to the PHP and MySQL stuff… Certainly, there are many things I don't know yet :D
What's wrong with this function?
Thanks!
function convertHashtags($str){
list($str1, $str2) = explode("#", $str) ;
$str2 = '#'.$str2.'';
$str = $str1." ".$str2 ;
return($str);
}
Can you use the above function and test with database entry?
Oh well, I just add new element to a database and it works!
I was testing it on old elements, they was inserted before I wrote the function.
I should try it before writing a this, my bad… thanks for help anyway!
$string = $row["content"];
$string = (string)$string;
$string = convertHashtags($string);
Use the above code and it will work.
yesterday i asked a question that how to select specific word from string which is having # sign with it.
someone told me this solution
$abc = "hello #john what are you doing";
$found = preg_match('/#([^-\s]*)/', $abc, $matches);
$name = null;
if ($found) {
$name = $matches[1];
}
it works like a charm but the problem is it only select first word with # sign if the string have alot of words like that. so now i need a loop which selects all the words in string which are having # sign with them.
Use can use preg_match_all to get all matches of your regular expression, not just the first one.
$abc = "hello #john what on earth are #stella and #steve doing";
$found = preg_match_all('/#([^-\s]*)/', $abc, $matches);
if ($found) {
foreach ($matches[1] as $name) {
echo "Name: $name", PHP_EOL;
}
}
Output:
Name: john
Name: stella
Name: steve
I have string that will look like this:
$string = "hello, my, name, is, az";
Now I just wanna echo whatever is there before first comma. I have been using following:
echo strstr($this->tags, ',', true);
and It has been working great, but the problem it only works php 5.3.0 and above. I am currently on PHP 5.2.
I know this could be achieve through regular express by pregmatch but I suck at RE.
Can someone help me with this.
Regards,
<?php
$string = "hello, my, name, is, az";
echo substr($string, 0, strpos($string, ','));
You can (and should) add further checks to avoid substr if there's no , in the string.
Use explode than,
$arr = explode(',', $string,2);
echo $arr[0];
You can explode this string using comma and read first argument of array like this
$string = "hello, my, name, is, az";
$str = explode(",", $string, 2);
echo $str[0];
$parts = explode(',',$string);
echo $parts[0];
You can simple use the explode function:
$string = "hello, my, name, is, az";
$output = explode(",", $string);
echo $output[0];
Too much explosives for a small work.
$str = current(explode(',', $string));
I am writing a program in PHP, and i need to find data that is in between two sets of symbols, and convert that to a string. For example
$main = "Hello, everyone, my name is (-Jack-)"
$string = regex_function('(-', $main) #should return "Jack"
How do i get that output, using a regex function or something
Try this :
$main = 'Hello, everyone, my name is (-Jack-)';
preg_match_all('/\(\-(?P<name>.*)\-\)/', $main, $matches);
echo "<pre>";
print_r($matches);
echo $matches['name'][0];
The function is known as preg_match_all().
$main = "Hello, everyone, my name is (-Jack-)";
preg_match_all('/\(\-(?P<name>\w+)\-\)/', $main, $string);
print_r( $string );
A sample output on codepad.
Referring to #Prasanth's comment; here's a better regex.
$main = "Hello, everyone, my name is (-Jack stuff-) some more text (-John stuff-)";
preg_match_all('/\(\-(?P<name>[\s\w]+)\-\)/', $main, $string);
print_r( $string );
Codepad link.
I want to get all Performance ID's from this page .
<?php
$content = file_get_contents("http://www124.popmundo.com/Common/Performances.asp?action=ComingPerformances&ArtistID=1962457");
$regex = "Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/s";
//all pattern variations tested, not working
if(preg_match_all($regex, $content, $m))
print_r($m);
else
echo "FALSE";
// this is returning FALSE
Use & instead of & in your regex.
Try this:
$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
It looks like an escape problem. Not knowing php, I would guess one of these
might fix it:
$regex = 'Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)';
or
$regex = "Performances\\.asp\\?action=Arrangements&PerformanceID=([0-9]+)";
or
$regex = '/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/';