Reputation:
Lets say I have a string variable goes like this:
$pro_details = 'Main Information: Goes Here';
Now I want to separate this variable into two pieces by the : sign. So one of the parts would be Main Information
and the other part is Goes Here
.
Then I want to make bold the 1st part Which is Main Information
. So the output of this variable after this process would be:
Main Information: Goes Here
Till now I just know how to break down a variable like this:
$pro_details = explode(':', $row_results['pro_details']);
But I don't know how to make bold the first part just like I told you.
So, if you know how to solve this, please let me know.. thanks in advance!
===========================================================================
UPDATE 1:
Since I have 3 variables like the one I discussed about it here, I coded this:
$pro_details1 = explode(':', $row_results['product_details1']);
$pro_details1[0] = "<strong>{$pro_details1[0]}</strong>";
$pro_details2 = explode(':', $row_results['product_details2']);
$pro_details2[0] = "<strong>{$pro_details2[0]}</strong>";
$pro_details3 = explode(':', $row_results['product_details3']);
$pro_details3[0] = "<strong>{$pro_details3[0]}</strong>";
And I tried echoing them like this:
if (!empty($pro_details1)||(!empty($pro_details2))||(!empty($pro_details3))){
echo "
<p>
<ul class='BHamid' style='font-size:25px;'>
";
if(!empty($pro_details1)){
echo "
<li>$pro_details1[0]:$pro_details1[1]</li>
";
}
if(!empty($pro_details2)){
echo "
<li>$pro_details2[0]:$pro_details2[1]</li>
";
}
if(!empty($pro_details3)){
echo "
<li>$pro_details3[0]:$pro_details3[1]</li>
";
}
echo "
</ul>
</p>
";
}
And then this error comes up:
Notice: Undefined offset: 1 on line 13
Line 13:
<li>$pro_details2[0]:$pro_details2[1]</li>
Because I set the 2nd pro_details
(which is $pro_details2
) empty in the table to see how this code works if one of the fields is empty.
So I need a way out of this error, because sometimes one of the pro_details
variables can be NULL due to the project that I'm working on.
Upvotes: 0
Views: 2538
Reputation: 4217
What about preg_replace:
$pro_details = 'Main Information: Goes Here';
echo preg_replace('/(.*):/', '<b>$1:</b>', $pro_details);
This captures everything before the colon and wraps it in tags.
Output:
<b>Main Information:</b> Goes Here
preg_replace uses regular expressions to search for patterns and to replace those patterns with something else. See the docs on the PHP site. preg_replace
Briefly:
The first arg is the search pattern. Books have been written on regular expressions so I won't go into it here other than to explain the example.
/(.*):/
means to search for a sequence of any character up to a colon ':'.
. means any character * means 0 or more of the preceding character.
The parens ()
are used to capture that sequence so it can be used in the replace side. The $1
in the second parameter is the sequence of characters that was found inside the parens in the search parameter.
If I wrote this in English, it would read as follows:
Find a sequence of characters up to but not including a colon. Replace that sequence of characters by prefixing <b>
and suffixing </b>
, leaving the rest of the string unmodified.
One other thing to note. In this specific case, we could actually use str_replace()
because we know the opening bold tag goes at the beginning.
echo '<b>' . str_replace(':', ':</b>', $pro_details);
Upvotes: 1
Reputation: 23389
Explode on the colon just like you said...
$pro_details = explode(':', $row_results['pro_details']);
Then wrap the first one in a <b>
tag which makes it bold when rendered in the broswer.
$pro_details[0] = "<b>{$pro_details[0]}</b>";
Then implode them again.
$pro_details = implode(':', $pro_details);
And there you go... echo $pro_details;
I would make a function so you're not repeating yourself so much.
function boldifyTitle($pd){
$pd = explode(':', $pd);
$pd[0] = "<b>$pd</b>";
if(empty($pd[1])) $pd[1] = "";
return implode(':', $pd);
}
echo "<li>".boldifyTitle($pro_details[0])."</li>";
Upvotes: 2