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);
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 problem. I need to get some lines of a page like this:
Text text text ...
Porto-Portugal-May-2013
Barcelona-Spain-April-2013
Text text text text text ...
Madrid-Spain-April-2013
Text text text ...
I need filter so that only appear the following:
Porto-Portugal-May-2013
Barcelona-Spain-April-2013
Madrid-Spain-April-2013
(lines with 3 dashes)
It’s possible with preg_match_all or other function?
I use cURL to get page content.
I have tried:
$body = " Text text text ...
Porto-Portugal-May-2013
Barcelona-Spain-April-2013
Text text text text text ...
Madrid-Spain-April-2013
Text text text ...";
preg_match_all("/^(.*?)-(.*?)-(.*?)-(.*?)\/",$body, $match);
for($i=0;$i<sizeof($match[1]);$i++)
{
echo $match[1][$j].'<br/>';
}
Thank you.
^ means "start of string".
Add the m modifier to make it mean "start of line" instead.
Then it's easier:
preg_match_all("/^(?:[^-\n]+-){3}[^-\n]+$/m",$body,$matches);
var_dump($matches[0]);
This should output an array containing each line that matched.
In the case of defined years in the last of your lines, you don't need regex to complete this task, as follows:
<?php
$yearsList = array(2013, 2014);
$body = " Text text text ...
Porto-Portugal-May-2013
Barcelona-Spain-April-2013
Text text text text text ...
Madrid-Spain-April-2013
Text text text ...";
$arr = explode("\n",$body);
$res = array();
foreach ($arr as $items){
$itemArr = explode('-', $items);
foreach ($itemArr as $item){
if (in_array($item, $yearsList)) $res[] = $items;
}
}
echo "<pre>";
print_r($res);
?>
View This DEMO: http://codepad.org/fdhwEJC4
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 been dealing with this for awhile and i could not figure out how to do the following in PHP:
$string = "this is test <pre>somrthing in <p>pharagraph</p></pre> dont know tell me <pre>value33 kooo ok</pre> this is php regress <pre>teeth value</pre> ok how";
function get_innercode($string) {
preg_match_all("#<pre>(.*?)</pre>#", $string, $foo);
echo implode("\n", $foo[1]);
}
$insert1=get_innercode($string);
$insert2=" 2 place me in the pre tag"; // you can ignore this
$string="this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how ";
how can i do this?
Please note i cannot do
$string="some text here <pre> WHERE $insert1 SHOULD BE</pre> some more text here <pre>WHERE $insert2 SHOULD BE</pre> ok how";
because i am getting $insert1 and $insert2 from $string to modify. i need to place it back in there where they come from.
thank you
It's pretty easy to do:
$insert1 =" 1 place me in the pre tag";
$insert2 =" 2 place me in the pre tag";
$string = "some text here <pre>{$insert1}</pre> some more text here <pre>{$insert2}</pre> and may be some more text or ";
You can echo a variable from inside a string when it's wrapped around double quotes. This does not work with single quotes.
Edit: This might be what you are looking for then:
$string = "This my <code>awesome</code> code";
$string = preg_replace('/<code>(.*?)<\/code>/', '<pre>$1</pre>', $string);
In php you can concatenate strings with a "." before and after, example:
$var1 = "hello";
$var2 = "What's up";
$concat = "Hey, ".$var1." ,".$var2."<br>";
echo $concat;
Similar methodology here. This allows for larger areas to be filled
$insert[0] = "Some Text";
$insert[1] = "Some More Text";
function Parser($content){
$i = 0;
while (preg_match_all('`\<pre\>(.*?)\</pre\>`', $content, $matches)) {
foreach ($matches[0] as $key => $match) {
//$innertext = $matches[1][$key]; //replace with your preferred filler
$innertext = $insert[$i]; //such as this
$replacement = '<pre>' . trim($innertext) . '</pre>';
$content = str_replace($match, $replacement, $content);
$i++;
}
}
}
Parse your line(s) of content like so:
Parser("this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how");
This is untested so may have a couple of bugs in it.
Try this,
I am adding div tag before each pre content. You can also modify content as needed by using $matches[1]
$string_new = preg_replace_callback(
'#<pre>(.*?)</pre>#',
function ($matches) {
return "<div>".$matches[0]."</div>";
},
$string
);
echo $string_new;
I have a bunch of chat logs that look like this:
name: some text
name2: more text
name: text
name3: text
I want to highlight the just the names. I wrote some code that should do it, however, I was wondering if there was a much cleaner way than this:
$line= "name: text";
$newtext = explode(":", $line,1);
$newertext = "<font color=red>".$newtext[0]."</font>:";
$complete = $newertext.$newtext[1];
echo $complete;
Looks fine, although you can save the temp variables:
$newtext = explode(":", $line,1);
echo "<font color=red>$newtext[0]</font>:$newtext[1]";
This might be faster or might not, you'd have to test:
echo '<font color=red>' . substr_replace($line, '</font>', strpos($line, ':') , 0);
The answer posted by gview is the simplest it gets, however and just as a reference you can use a regular expression to find the name tag, and replace it with the new html code using preg_replace() as follows:
// Regular expression pattern
$pattern = '/^[a-z0-9]+:?/';
// Array contaning the lines
$str = array('name: some text : Other text and stuff',
'name2: more text : : TEsting',
'name: text testing',
'name3: text Lorem ipsum');
// Looping through the array
foreach($str as $line)
{
// \\0 references the first pattern match which is "name:"
echo preg_replace($pattern, "<font color=red>\\0</font>:", $line);
}
also try the RegExp like this:
$line = "name: text";
$complete = preg_replace('/^(name.*?):/', "<font color=red>$1</font>:", $line);
echo $complete ;
EDIT
if their names aren't "name" or "name1", just delete the name in pattern, like this
$complete = preg_replace('/^(.*?):/', "<font color=red>$1</font>:", $line);