This is the string
(code)
Pivot: 96.75<br />Our preference: Long positions above 96.75 with targets @ 97.8 & 98.25 in extension.<br />Alternative scenario: Below 96.75 look for further downside with 96.35 & 95.9 as targets.<br />Comment the pair has broken above its resistance and should post further advance.<br />
(text)
"Pivot: 96.75
Our preference: Long positions above 96.75 with targets @ 97.8 & 98.25 in extension.
Alternative scenario: Below 96.75 look for further downside with 96.35 & 95.9 as targets.
Comment the pair has broken above its resistance and should post further advance.
"
the result should be
(code)
<b>Pivot</b>: 96.75<br /><b>Our preference</b>: Long positions above 96.75 with targets @ 97.8 & 98.25 in extension.<br /><b>Alternative scenario</b>: Below 96.75 look for further downside with 96.35 & 95.9 as targets.<br />Comment the pair has broken above its resistance and should post further advance.<br />
(text)
Pivot: 96.75
Our preference: Long positions above 96.75 with targets @ 97.8 & 98.25 in extension.
Alternative scenario: Below 96.75 look for further downside with 96.35 & 95.9 as targets.
Comment the pair has broken above its resistance and should post further advance.
The porpuse:
Wrap all the words before :
sign.
I've tried this regex: ((\A )|(<br />))(?P<G>[^:]*):
, but its working only on python environment. I need this in PHP:
$pattern = '/((\A)|(<br\s\/>))(?P<G>[^:]*):/';
$description = preg_replace($pattern, '<b>$1</b>', $description);
Thanks.
I should start by saying that HTML operations are better done with a proper parser such as DOMDocument
. This particular problem is straightforward, so regular expressions may work without too much hocus pocus, but be warned :)
You can use look-around assertions; this frees you from having to restore the neighbouring strings during the replacement:
echo preg_replace('/(?<=^|<br \/>)[^:]+(?=:)/m', '<b>$0</b>', $str);
First, the look-behind assertion matches either the start of each line or a preceding <br />
. Then, any characters except the colon are matched; the look-ahead assertion makes sure it's followed by a colon.
The /m
modifier is used to make ^
match the start of each line as opposed to \A
which always matches the start of the subject string.
This preg_replace should do the trick:
preg_replace('#(^|<br ?/>)([^:]+):#m','$1<b>$2</b>:',$input)
PHP Fiddle - Run (F9)
The most "general" and least regex-expensive way to do this that I could come up with was this:
$parts = explode('<br', $str);//don't include space and `/`, as tags may vary
$formatted = '';
foreach($parts as $part)
{
$formatted .= preg_replace('/^\s*[\/>]{0,2}\s*([^:]+:)/', '<b>$1</b>',$part).'<br/>';
}
echo $formatted;
Or:
$formatted = array();
foreach($parts as $part)
{
$formatted[] = preg_replace('/^\s*[\/>]{0,2}\s*([^:]+:)/', '<b>$1</b>',$part);
}
echo implode('<br/>', $formatted);
Tested with, and gotten this as output
Pivot: 96.75
Our preference: Long positions above 96.75 with targets @ 97.8 & 98.25 in extension.
Alternative scenario: Below 96.75 look for further downside with 96.35 & 95.9 as targets.
Comment the pair has broken above its resistance and should post further advance.
That being said, I do find this bit of data weird, and, if I were you, I'd consider str_replace
or preg_replace
-ing all breaks with PHP_EOL
:
$str = preg_replace('/\<\s*br\s*\/?\s*\>/i', PHP_EOL, $str);//allow for any form of break tag
And then, your string looks exactly like the data I had to parse, and got the regex for that here:
$str = preg_replace(...);
$formatted = preg_replace('/^([^:
\\]++)\s{0,}:((
(?![^
:\\]++\s{0,}:)|.)*+)/','<b>$1:</b>$2<br/>', $str);