PHP - Highlight character(s) in string [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm currently working on a search function for my website.
When the user is searching 'Test', and someone's name is 'besttest', for example, the 'test' in 'besttest' should be in another color. But only the 'test', not the whole word.
I've tried to figure it out myself, but I didn't get it working.
Hope you guys and girls can help me ^^

Use the preg_replace() function. The example below will highlight just the keywork wish is "Test" in this case not the whole word. Try this code, really it will help you.
$str = "besttest";
$keyword = "Test";
$str = preg_replace("/($keyword)/i",'<span class="yellow">$1</span>',$str);
print($str);
this will output something like this :
best<span class="yellow">test</span>

It makes only sense, if you use a markup language... but here's an approach:
$lookup = [
'test' => '<span class="red">%s</span>',
'color' => '<span class="green">%s</span>',
'[0-9]+' => '<b>%s</b>',
];
$string = 'This is a test for coloring testwise an uncolored test-value. Testing 950 349 2 numbers... ';
echo 'before: "'. $string.'"'.PHP_EOL;
foreach($lookup as $term => $format) {
$string = preg_replace_callback('/'.$term.'/', function($matches) use($format) {
return sprintf($format, $matches[0]);
}, $string);
}
echo 'after: "'. $string.'"'.PHP_EOL;
Output:
before: "This is a test for coloring testwise an uncolored test-value. Testing 950 349 2 numbers... "
after: "This is a <span class="red">test</span> for <span class="green">color</span>ing <span class="red">test</span>wise an un<span class="green">color</span>ed <span class="red">test</span>-value. Testing <b>950</b> <b>349</b> <b>2</b> numbers... "
With regular expressions, you can match pretty much anything possible and give it different styles...
hth

Related

HTML tags replacing with php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
If I have a paragraph:
echo "^b(This sentence becomes bold), and ^i(this becomes italic).\nThen this becomes ^up(uppercase).";
how to replace ^b, ^i, ^up, \n into a HTML tags ?
This sentence becomes bold, and this becomes italic.
Then this becomes UPPERCASE.
Thankyou.
You could try to use preg_replace() with patterns to do this:
<?php
// your example text
$text = "^b(This sentence becomes bold), and ^i(this becomes italic).\nThen this becomes ^up(uppercase).";
// array of patterns
$patterns = [];
$patterns[0] = "/\^b\((.*?)\)/";
$patterns[1] = "/\^i\((.*?)\)/";
$patterns[2] = "/\^up\((.*?)\)/";
// array of replacements
$replacements = [];
$replacements[0] = '<b>${1}</b>';
$replacements[1] = '<i>${1}</i>';
$replacements[2] = '<span style="text-transform:uppercase;">${1}</span>'; // or use something better here
// process the text
$formattedText = preg_replace($patterns, $replacements, $text);
// see the result
echo $formattedText;
?>
It would be much better if you write this logic in a helper function so you could use it easier on different places later.

How I can replace <br /> with " " in a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php
function show(){?>
<?php echo "a"; ?> <br />
}
//this function is in another file
<?php
echo str_replace("<br />"," ",show());//search for <br />
?>
How i can replace the <br /> with " "?
You need to buffer the output. Something similar to this:
ob_start();
show();
echo str_replace("<br />", " ", ob_get_clean());
You can use a callback in combination with ob_start. The callback will be called each time the output is flushed.
function replace_br($buffer)
{
return preg_replace('~<br\b[^>]*>~i', ' ', $buffer);
}
ob_start('replace_br');
The regular expressions says:
find the string '
the char after should not be alphanumeric
find any characters others than '>'
find an '>'
This replaces <br>, <BR>, <br/>, <br /> but also something like <br class="clearfix">.

Extract gallery ID from HTML PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a variable named $html:
$html = '<li class="col">[gallery_id=234]</li>';
I want to get gallery_id (in this case - 234) into another variable from $html.
Easy. Just use regex with preg_match:
$html = '<li class="col">[gallery_id=234]</li>';
preg_match("/\[gallery_id=([0-9].*)\]/is", $html, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
I have that print_r in there for debugging/illustration purposes. It would return the following:
Array
(
[0] => [gallery_id=234]
[1] => 234
)
Then to access the result you want, just do this:
echo $matches[1];
The returned value will be:
234
$html = '<li class="col">[gallery_id=234]</li>';
preg_match('!\d+!', $html, $var);
echo $var[0]; //echoes 234

remove words between / Slash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to delete words bettwen slashes
I have this string:
This a test UP/PL/EX/TU 2013
this a test 2 MG/MF/RS/TB 2007
I need this output
This a test 2013
this a test 2 2007
The string is dinamically, always changes.
CAN BE DONE WHIT REGEX ?
I'm sure there's a better expression but given the strings in the question this might be good enough.
$string='This a test UP/PL/EX/TU 2013';
$output=preg_replace("/\s[\w\/]+\s/", " ", $string);
echo $output;
$s1 = 'This a test UP/PL/EX/TU 2013';
$s2 = ' this a test 2 MG/MF/RS/TB 2007';
$regex = '|\s*(?:[[:alnum:]]+/)+[[:alnum:]]+\s*|';
echo "$s1 => '", preg_replace($regex, ' ', $s1), "\n";
echo "$s2 => '", preg_replace($regex, ' ', $s2), "\n";
Output:
This a test UP/PL/EX/TU 2013 => 'This a test 2013
this a test 2 MG/MF/RS/TB 2007 => ' this a test 2 2007
HTH
You can use this:
$result = preg_replace('~\h*+\w*+\/(?>\w+\/?)++\h*+~', ' ', $string);

wordpress the_title will split [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
hello I am creating my own theme on wordpress and i want to split the the_title() function into array all i want is the last item on array will be on <span> and the other one is on <h1>.
i tried this
$str = the_title();
$val = explode(" ", $str); // also tried implode
echo "<pre>";
print_r($val);
echo "</pre>";
but it only return array with no items
hope someone will help me.
this wat i really want to be the output
<h1> This is a <span>Title</spam></h1>
thanks in advance
Dont use the_title() as it automatically display the current title of the page.
Beside this Use $post->post_title;.
Lets suppose a sample sentence This is a Title
<?php
global $post;
$str = $post->post_title;
$exp = explode(" ",$str);
echo "<h1>".$exp[0].$exp[1].$exp[2];
echo "<span>".$exp[3]."</span></h1>";
?>
Output will be:
<h1>This is a<span>title</span></h1>
Please use get_the_title() instead of the_title().
Beside is the link for complete understanding :--
http://codex.wordpress.org/Function_Reference/get_the_title
Use following instead of $str = the_title();
$str = get_the_title();
-or-
$str = the_title('', '', false);
documentation : http://codex.wordpress.org/Function_Reference/get_the_title

Categories