Jump to content

Highlight text if preg_match dot(.)


balkan7

Recommended Posts

Hi guys

I need highlight text from variable like this one:

 

$text = "Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat.";

  If preg_match first dot in text i want replace it like this bellow:

 

  Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat.

Link to comment
Share on other sites

So, basically you want to add a newline after the first period?

 

No need for regular expressions for this one, str_replace handles that just fine:

$text = str_replace ('. ', ".\n", $text, 1);
Or, upon re-reading your post. Seems like you want to add bold tags to it too. (Instead?)

If so, then you'd need a RegExp. Only, not preg_match, but preg_replace:

$regExp = '/^([^.]+)\\./';
$replace = '<strong>$1</strong>.';
$text = preg_replace ($regExp, $replace, $text);
Edited by Christian F.
Link to comment
Share on other sites

Tested and working, but if exist date like 05.10.2011 highlighting stop in 05. can be this skip ?

 

And text separated with two variables:

$highlighted = "Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
$more = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat."; 
Link to comment
Share on other sites

$text= "Quis nostrud 05.10.2011 exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat.";
$regExp = '/^([^.]+)\\.\s/';
$replace = '<strong>$1</strong>.';
$text = preg_replace ($regExp, $replace, $text);
$test = explode(".", $text);
$display1 = nl2br($test[0]);
$display2 = nl2br($test[1]);

Result is: Quis nostrud 0510  (without apply tags <strong>)

Link to comment
Share on other sites

Ah, now I see.

Change the [^.]+ part to .*?, and it should work.

 

Also, you missed a character in the string I said you needed to explode on. Which is why you're only getting the first part of the date, instead of the first line, after the explode.

Edited by Christian F.
Link to comment
Share on other sites

Result for dates is: Quis nostrud 0510

Result for non dates is: Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat.

Link to comment
Share on other sites

balkan7, are you able to install PECL extensions? I only ask because as part of the Intl extension (and to be bundled with PHP 5.5) are some classes to make tasks like breaking up text into sentences super-easy. If you can, putting them to use would mean installing Intl 3 (currently 3.0.0b1) via

pecl install intl-beta

Then, playtime!

<?php

$text = 'Quis nostrud 05.10.2011 exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat. Aenean vel arcu at libero vestibulum bibendum. Integer sit amet lacus nibh, viverra feugiat ante. Ut justo libero, venenatis at tincidunt eget, dapibus vitae velit. Aliquam tempor, urna sed bibendum pharetra, metus diam vestibulum augue, sed adipiscing tortor ante vitae lacus. Donec congue egestas ipsum, sit amet porta lectus pellentesque at. ';

$sentence_iterator = IntlBreakIterator::createSentenceInstance('en');
$sentence_iterator->setText($text);
$sentences = iterator_to_array($sentence_iterator->getPartsIterator());

var_export($sentences);

?>

 

This shows that $sentences is an array:

 

array (
0 => 'Quis nostrud 05.10.2011 exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ',
1 => 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat. ',
2 => 'Aenean vel arcu at libero vestibulum bibendum. ',
3 => 'Integer sit amet lacus nibh, viverra feugiat ante. ',
4 => 'Ut justo libero, venenatis at tincidunt eget, dapibus vitae velit. ',
5 => 'Aliquam tempor, urna sed bibendum pharetra, metus diam vestibulum augue, sed adipiscing tortor ante vitae lacus. ',
6 => 'Donec congue egestas ipsum, sit amet porta lectus pellentesque at. ',
)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.