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
Related
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
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 want to cut text before "DETAIL" .
<?PHP
$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds "
//i want result Name:myproduct Price:99
?>
You are going to need to use substr which is used to get the substring of a string along with strpos which gives the position of the given occurance:
echo substr($text, 0, strpos($text, 'DETAIL'));
Visit php.net
You can split the text in array and get the first element of array:
$text="Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds ";
$abc = explode('DETAIL',$text);
echo $abc[0];
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, 0, stripos($text, "detail"));
echo $clean;
This will output
Name:myproduct Price:99
$text = "Name:myproduct Price:99 DETAIL :abcdedsfsdff 21348dfsdf ds";
$clean = substr($text, stripos($text, "detail"), strlen($text) - 1);
echo $clean;
This will output
DETAIL :abcdedsfsdff 21348dfsdf ds
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
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")
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
For exemple, it's possible to replace
{'a1b2,left'}
with
<img src="a1b2" class="left"/>
in Php?
Yes...
$str = preg_replace('/\{\'(.*?),(.*?)\'\}/', '<img src="$1" class="$2"/>', $str);
yes you can.
//strip unwanted characters.
$string = preg_replace("/[{'}]/", "", $string);
//convert string to array
$string = explode(',', $string);
<img src="<?php echo $string[0]; ?>" class="<?php echo $string[1]; ?>"/>