I'm getting a text in a variable as follows:
$text = 'This is a code snippet';
Now I want to enclose the above text within [cdata]...[!cdata].
The output of echo $text should be like below:
[cdata]This is a code snippet[!cdata]
How should I do this?
Thanks.
Try
$text = 'This is a code snippet';
$text = '[cdata]'.$text.'[!cdata]';
echo $text;
Try this
$text = "This is a code snippet";
$text = "[cdata]{$text}[!cdata]";
echo $text;
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 want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"
Good day.
We have code:
function testfunc($text){
return $text;
}
$text = "Simple text {testfunc(1)} and next simple text and we {testfunc(20)} are happy ";
// number in {testfunc()} only for example - it can be other number or text.
Tell me please how replace {testfunc(1)} on result function testfunc($num)?
P.S.: In result need get next text:
$text = "Simple text 1 and next simple text and we 20 are happy ";
Try this:
$newText = preg_replace_callback("/\{testfunc\((\d+)\)\}/","testfunc",$text);
And you'll get array of matches in your "testfunc". For what you want, this code:
I think, it helps you.
<?php
function testfunc($text){
return $text[1];
}
$text = "Simple text {testfunc(1)} and next simple text and we {testfunc(20)} are happy ";
$newText = preg_replace_callback("/\{testfunc\((\d+)\)\}/","testfunc",$text);
echo $text."\n";
echo $newText."\n";
?>
This should work, using string concatenation:
$text = "Simple text " . testfunc(1) . " and next simple text and we " . testfunc(20) . " are happy ";
Try
$text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are happy ";
As normal why not try with,
$text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are happy ";
<?php
$text = "Simple text ".testfunc(1)." and next simple text and we ".testfunc(20)." are
happy";
echo $text;
?>
I have some text like the following example:
Some Text Here
[code]Some link[/code]
Text
[code]Link[/code]
Other Text
[code]Another Link[/code]
Other Text1
I want to remove all the text above, under, and between the two code. Here's an example of the output I want:
[code]Some Link[/code]
[code]Link[/code]
[code]Another Link[/code]
I use preg_replace for removing text above the first Code, in this way:
$message = preg_replace('/(.*?)\[code/si','[code',$message, 1);
Can you help me to remove the other text, using preg_replace?
You can do this way:
preg_match_all('/(\[code\].*\[\/code\])/Usmi', $text, $res);
$cnt = 0;
foreach ($res as $val) {
$cnt++;
$message .= $val[$cnt] . "<br />";
}
echo $message;
Just to make the solution of #Andreev a little more simple :
$text = "
Some Text Here
[code]Some link[/code]
Text
[code]Link[/code]
Other Text
[code]Another Link[/code]
Other Text1
";
$keywords = preg_match_all('/(\[code\].*\[\/code\])/Usmi', $text, $res);
print(implode($res[0]));
You can test it here : http://phptester.net/index.php?lang=en
Assuming you can never have [code] abc [code] def [/code] ghi [/code], try this:
do {
$message = preg_replace("((?:\[code\].*?\[/code\])*).*?(?=\[code\]))is","$1",$message,-1,$c);
} while($c);
Using PHP, given a string such as: this is a <strong>string</strong>; I need a function to strip out ALL html tags so that the output is: this is a string. Any ideas? Thanks in advance.
PHP has a built-in function that does exactly what you want: strip_tags
$text = '<b>Hello</b> World';
print strip_tags($text); // outputs Hello World
If you expect broken HTML, you are going to need to load it into a DOM parser and then extract the text.
What about using strip_tags, which should do just the job ?
For instance (quoting the doc) :
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
will give you :
Test paragraph. Other text
Edit : but note that strip_tags doesn't validate what you give it. Which means that this code :
$text = "this is <10 a test";
var_dump(strip_tags($text));
Will get you :
string 'this is ' (length=8)
(Everything after the thing that looks like a starting tag gets removed).
strip_tags is the function you're after. You'd use it something like this
$text = '<strong>Strong</strong>';
$text = strip_tags($text);
// Now $text = 'Strong'
I find this to be a little more effective than strip_tags() alone, since strip_tags() will not zap javascript or css:
$search = array(
"'<head[^>]*?>.*?</head>'si",
"'<script[^>]*?>.*?</script>'si",
"'<style[^>]*?>.*?</style>'si",
);
$replace = array("","","");
$text = strip_tags(preg_replace($search, $replace, $html));