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);
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"
Anybody knows how i can achieve this:
I want post message and colorize everything between " * " Tags.
Like this:
This [*]is[*] test [*]message[*] :)
To:
This [yellow]is[/yellow]> test [yellow]message[/yellow] :)
I wroted something like this to achieve my goal:
if(preg_match_all('/\*(.*?)\*/',$message,$match)) {
$beforemessage = explode("*", $message, 2);
$message = $beforemessage[0]. " <font color='yellow'>" .$match[0][0]. "</font>";
}
Howewer it returns only:
This [yellow]is[yellow]
Just use preg_replace():
$message = "This *is* test *message*";
echo preg_replace('/\*(.*?)\*/', '<font color="yellow">$1</font>', $message);
This <font color="yellow">is</font> test <font color="yellow">message</font>
preg_match_all returns an array of matches, but your code only ever replaces the FIRST match in that array. You'd have to loop over the array to handle the OTHER matches.
There are a few approaches when using regular expressions.
One is to do matching - keeping track of the position of the match and the length of the match. Then you can split up the original message into substrings and concatenate it all back together.
The other is to do search/replace using regular expressions.
Try this, or similar approach:
<?php
$text = "Hello hello *bold* foo foo *fat* foo boo *think* end.";
$tagOpen = false;
function replaceAsterisk($matches) {
global $tagOpen;
$repl = "";
if($tagOpen) {
$repl = "</b>";
} else {
$repl = "<b>";
}
$tagOpen = !$tagOpen;
return $repl;
}
$result = preg_replace_callback( "/[*]/", "replaceAsterisk", $text);
echo $result;
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 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);