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.
Related
I've tried:
$input = str_replace('[IMG]',"",$input,$rplc);
with no success.
I've also tried escaping and double-escaping [IMG] by doing '[IMG]' and '\[IMG\]', neither worked. It doesn't throw any errors, but it doesn't actually replace anything either.
How can I get this to work?
Have you tried something like:
$input = preg_replace("/(\\[IMG\\])/", $replacement, $input);
Just try this example, to implement within your code..
$search = "[IMG]";
$replace = "";
$string = "Hello [IMG] Approxx? How[IMG] are [IMG]You?";
$input = str_replace($search, $replace, $string);
echo $input;
Try this
preg_replace("#\[img\]#","",$input);
And the same result can also be achived by using
str_replace()
str_replace("[img]",'',$input);
I'm trying to split a php string in to parts the first one include the delimiter but the second one doesn't
$string = "abc==123";
What I want exactly is to get
$string['0'] = "abc==";
$string['1'] = "123";
Thanks for help
Simple enough
<?php
$string = explode("==", $string);
$string[0] .= "==";
?>
I believe you want the function strstr
http://us1.php.net/strstr
You can use PHP's explode function,
$data = "abc==123";
$result = explode("==", $data);
echo $result[0]. "==";
echo $result[1];
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]+)/';