insert </br> after comma with php [closed] - php

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 array text from custom field "black, grey, white" with this code:
<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'colors', true); ?>
I want show me like this:
black</br>
grey</br>
white
It's possible with PHP? Many thanks

Use str_replace():
echo str_replace(",", "<br />", get_post_meta($postid, 'colors', true));

If you want to add <br> after a "," you can do this:
$text = preg_replace("/,/", "<br>", get_post_meta($postid, 'colors', true));

str_replace(", " , ", <br />" , $string) ;
The format is ("from", "to" , "what")

Related

Remove underscore from php echo output [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 8 years ago.
Improve this question
How do i remove underscore from this echo?
if($validation->fails())
{
echo '<div class="error_message">', ($validation->errors()->first('first_name')), '</div>';
}
Thank you.
what madness is this?
if($validation->fails()) {
echo '<div class="error_message">' . str_replace("_"," ",($validation->errors()->first('first_name'))) . '</div>';
}
To replace a string char you have to use str_replace function.
Here is an example:
$var = "Hello_World";
$var = str_replace("_"," ",$var);
echo $var;
Output:
Hello World

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

Limited Characters on a line [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 8 years ago.
Improve this question
I am designing a small application which shoots out status-like tweets from users at the five latest ones. To keep it a small application, I need for it to accept only so many characters on a line and the drop a line just below it. So for example:
Noah: The best thing about Stackoverflow
is that it is full of amazing programmers.
Something along the lines of something like that above. Can you help me with the code below :
echo "<div style='position:relative;top:-20px;padding-top:10px;padding-left:30px;padding-right:30px;'>";
echo "<p> $first_name: $body. </p>";
if (strlen($body <= 100)) {
echo "\n";
}
echo "</div>";
}
wordwrap will do that for you
string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )
Example:
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);
echo "$newtext\n";
A very
long
wooooooo
ooooord.

Categories