I have this string:
mr (3_22)
I want php to output that string to:
(3_22)
How can i do that with PHP? I need a sample code please
Try the following code:
$str ='mr (3_22) mrs (1_12) miss (2_4)';
$re ='';
if(preg_match_all('/(\([^\)]+\))/i', $str, $mt)){
$re = implode('', $mt[0]);
}
echo $re; // (3_22)(1_12)(2_4)
Related
I have a data base with texts and in each text there are words (tags) that start with # (example of a record : "Hi I'm posting an #issue on #Stackoverflow ")
I'm trying to find a solution to add html code to transform each tag into a link when printing the text.
So the text are stored as strings in MySQL database like this :
Some text #tag1 text #tag2 ...
I want to replace all these #abcd with
#abcd
And have a final result as follow:
Some text #tag1 text #tag2 ...
I guess that i should use some regex but it is not at all my strong side.
Try the following using preg_replace(..)
$input = "Hi I'm posting an #issue on #Stackoverflow";
echo preg_replace("/#([a-zA-Z0-9]+)/", "<a href='targetpage.php?val=$1'>#$1</a>", $input);
http://php.net/manual/en/function.preg-replace.php
A simple solution could look like this:
$re = '/\S*#(\[[^\]]+\]|\S+)/m';
$str = 'Some text #tag1 text #tag2 ...';
$subst = '#$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Demo
If you are actually after Twitter hashtags and want to go crazy take a look here how it is done in Java.
There is also a JavaScript Twitter library that makes things very easy.
Try this the function
<?php
$demoString1 = "THIS is #test STRING WITH #abcd";
$demoString2 = "Hi I'm posting an #issue on #Stackoverflow";
function wrapWithAnchor($link,$string){
$pattern = "/#([a-zA-Z0-9]+)/";
$replace_with = '<a href="'.$link.'?val=$1">$1<a>';
return preg_replace( $pattern, $replace_with ,$string );
}
$link= 'http://www.targetpage.php';
echo wrapWithAnchor($link,$demoString1);
echo '<hr />';
echo wrapWithAnchor($link,$demoString2);
?>
I have a string which contains n\ by mistake (as imported from csv). So,I just want to replace n\ with \n.
Possible conditions : n\,\n\n, n\\n,n\n\
$string = "hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n";
Try this. It will remove all the possible matches as per your question:-
$str = "main_string_goes_here";
$replace = "n\,\n\n,n\n\,\nn\,n\\n";
$arr = explode(",",$replace);
foreach($arr as $value)
{
str_replace($value,"\n",$str);
}
Happy Coding :-)
Use this:
str_replace("n\","\n",$string);
Here we search for the string, find the value "n\" and then replace the value with "\n". Update this example with your conditions.
Try like this
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$string = str_replace('n\\n\\','&new*',$string);
$string = str_replace('\\n\\n','&old*',$string);
$string = str_replace('n\\','\\n',$string);
$string = str_replace('&new*','\\n\\n',$string);
echo $string = str_replace('&old*','\\n\\n',$string);
Live demo : https://eval.in/904353
As other says to replace "n\" to "\n" will not work. You need to escape \ backslash also
Use this:
<?php
echo $string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
echo $text = str_replace('n', '', $new_str);
?>
You can try this.
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
echo str_replace('n\\', '\n',$string );
I have a string, that look like this "<html>". Now what I want to do, is get all text between the "<" and the ">", and this should apply to any text, so that if i did "<hello>", or "<p>" that would also work. Then I want to replace this string with a string that contains the string between the tags.
For example
In:
<[STRING]>
Out:
<this is [STRING]>
Where [STRING] is the string between the tags.
Use a capture group to match everything after < that isn't >, and substitute that into the replacement string.
preg_replace('/<([^>]*)>/, '<this is $1>/, $string);
here is a solution to test on the pattern exists and then capture it to finally modify it ...
<?php
$str = '<[STRING]>';
$pattern = '#<(\[.*\])>#';
if(preg_match($pattern, $str, $matches)):
var_dump($matches);
$str = preg_replace($pattern, '<this is '.$matches[1].'>', $str);
endif;
echo $str;
?>
echo $str;
You can test here: http://ideone.com/uVqV0u
I don't know if this can be usefull to you.
You can use a regular expression that is the best way. But you can also consider a little function that remove first < and last > char from your string.
This is my solution:
<?php
/*Vars to test*/
$var1="<HTML>";
$var2="<P>";
$var3="<ALL YOU WANT>";
/*function*/
function replace($string_tag) {
$newString="";
for ($i=1; $i<(strlen($string_tag)-1); $i++){
$newString.=$string_tag[$i];
}
return $newString;
}
/*Output*/
echo (replace($var1));
echo "\r\n";
echo (replace($var2));
echo "\r\n";
echo (replace($var3));
?>
Output give me:
HTML
P
ALL YOU WANT
Tested on https://ideone.com/2RnbnY
I have a string in the pattern similar to:
john.smith 9.5 9.49296 Active john.s#site.com +123456789
and I just want to echo "9.5" out of it which is next to "smith" using PHP.
Update:
sorry guys...just noticed that it is an xml file, couldn't see properly in safari, checked in firefox and it is displayed as:
<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s#site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>
Now what would be the php code to echo "9.5"
Thanks for your earlier answers...
Try splitting on whitespace (seem to be delimited by that)
$parts = preg_split('/\s+/', $input);
print $parts[1];
a quick and durty way
<?php
$string="john.smith 9.5 9.49296 Active john.s#site.com +123456789";
$array = explode(" ",$string);
echo $array[1];
?>
$string="john.smith 9.5 9.49296 Active john.s#site.com +123456789";
$array=explode(" ",$string);
Then your number would be:
echo $array[1];
This will work
<?php
$string = "<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s#site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>";
$xml = simplexml_load_string($string);
echo $xml->Balance;
?>
I want to add space to word something like this
CountryName
RegionName
ZipPostalCode
to be
Country Name
Region Name
Zip Postal Code
Let me know how can be done with php
You can use regular expressions to find [lowercase character][uppercase character] and insert a space:
$newstr = preg_replace('/([a-z])([A-Z])/s','$1 $2', $oldstr);
You might look into CakePHP's Inflector class for guidance (for example the humanize function).
Are they all camelCase like that? You can turn it into an array, then turn that into a string.
<?php
function splitCamelCase($str) {
return preg_split('/(?<=\\w)(?=[A-Z])/', $str);
}
print_r(splitCamelCase("ZipPostalCode"));
?>
Edit: Disregard this - Mark's answer is better.
$new = preg_replace('/([a-z])([A-Z])/', '$1 $2', $old);
Use preg_replace()
$str = 'HelloThere';
$str= preg_replace('/(?<=\\w)(?=[A-Z])/'," $1", $str);
echo trim($str); //Hello There
<?php
// It can be done as:
echo 'Country ','Name <br>';
echo 'Region ','Name <br>';
echo 'Zip ','Postal ','Code';
// OR
echo 'Country ','Name <br> Region ','Name <br> Zip ','Postal ','Code';
?>