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">.
Related
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 1 year ago.
Improve this question
all I want to know what's the syntax error in this code.
echo "<p id='hide_text_$heading->id' >"preg_replace($rx, "<b>$0</b>", $heading->shortHeadline)"</p>";
Use . to concatenate strings. Change your code to
echo "<p id='hide_text_$heading->id' >" . preg_replace($rx, "<b>$0</b>", $heading->shortHeadline) . "</p>";
to concatenate the three parts or separate each statement:
echo "<p id='hide_text_$heading->id' >";
echo preg_replace($rx, "<b>$0</b>", $heading->shortHeadline);
echo "</p>";
More on String Operators
you forget concatination sympol => .
Also there is syntax error in $0 if you mean by that variable name.
variable name could not start with numbers
only _ and string characters as i remember.
echo "<p id='hide_text_".$heading->id."' >" . preg_replace($rx, "<b>.$0.</b>", $heading->shortHeadline) . "</p>";
You can read more about Concatenation
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
I'm using the explode function in tandem with the implode to take a CSV text and make it display clearly on screen, like so:
$CSV_STRING = 'Quarterly update,,
Question Topic,Question Title,Question Text
Question Topic,Question Title,Question Text';
----------------------
$array = explode(PHP_EOL, $CSV_STRING);
$arrayImploded = implode("<br>", $array);
$arrayExploded = explode(",", $arrayImploded);
$arrayFinal = implode("<br>", $arrayExploded);
echo $arrayFinal;
My issue here is that I'm trying to detect the new line with PHP_EOL and it doesn't seem to detect it. I've used \n and \n\r to similar results.
What should I use to correctly display my results like so:
Quarterly update
Question Topic
Question Title
Question Text
Question Topic
Question Title
Question Text
Thank you!
Rather than exploding/imploding, you should be able to run it through nl2br() which will convert the new lines to <br /> tags and then if you just use str_replace() to replace the commas with <br /> tags as well, it should display as you want...
$CSV_STRING = 'Quarterly update,,
Question Topic,Question Title,Question Text
Question Topic,Question Title,Question Text';
$out = nl2br($CSV_STRING);
$out = str_replace("<br />", "<br /><br />", $out);
echo str_replace(",", "<br>", $out);
To create an extra break between the blocks, this just adds another <br /> tag to each one just added.
Note that I've used <br> rather than <br /> so you should see which part of the code is changing which part of the original string. But use <br /> for 'correctness'.
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 8 years ago.
Improve this question
I have a small program designed to out put the date
<?php
echo '<p>Ordered on ';
echo date('F jS g:iA');
echo '</p>';
?>
This outputs "Ordered on December 6th 5:25PM", how do i get the function to output "Ordered on December 6th at 5:25PM" instead?
This should work for you:
echo '<p>Ordered on ';
echo date('F jS \a\t g:iA');
echo '</p>';
try this:
<?php
echo '<p>Ordered on ';
echo date('F jS'), ' at ', date('g:iA');
echo '</p>';
?>
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