Jump to content

preg_match for bbcode (bold, underline, code - only)


AndyPSV

Recommended Posts

_italic_ **bold** `code`

 

how to extract it from the text & replace?

 

if for example ***** (five) would be used I want only one inside of it to be <b>*</b> bold; the same with others; how to do it?

 

thank you?

 

---

$s = 'There _are_ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. **Without those 2 points** you can be sure, that your depressio';

$output = 'There <i>are</i> few things which you <code>need to ensure</code>` you would 1. educate (read all) 2. put into to use. <b>Without those 2 points</b> you can be sure, that your depressio

Link to comment
Share on other sites

Hi Andy,

 

I took a pass at your problem and tested the code below.

 

Doing two replace operations in a row: one for the Italics, one for the Bold.

The "plus quantifier" ensures we can have multiple underscores and stars.

Last, the htmlentities in the echo ensure you can see the actual string in the html output (not the browser's rendering).

 

<?php 
$s = 'There ___are__ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';
$s = preg_replace('/_+([^_]*)_+/m', '<i>\1</i>', $s);
$s = preg_replace('/\*+([^*]*)\*+/m', '<b>\1</b>', $s);
echo htmlentities($s);
?>

 

Is this what you're looking for?

 

Wishing you a beautiful day. :)

Link to comment
Share on other sites

Yes, thank you for your help.

 

How can you do: "`" that does <code>

 

$s = preg_replace('/`+([^`]*)`+/m', '`\1`', $s);

but it don't works

 

?

 

BTW. How can you do that only it takes "exact", that only takes first internal, e.g. ** (replaces as <b>) and shows <b>*string some

 

Thank you

Link to comment
Share on other sites

$s = 'There ___are__ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';

 

should result in:

 

$output = 'There <u>__are_</u> few things which you <code>need to ensure</code>` you would 1. educate (read all) 2. put into to use. <b>*Without those 2 points</b> you can be sure, that your depressio';

 

the <code> part is missing and I don't know how to do it? Also I've wanted to leave all the signs which not represent exact match, for example: ___are__ (only first "_" on the edges are converted)

 

thank you

Link to comment
Share on other sites

Ah, now I get it.

Thanks for explaining.

 

I tested this code. It works for me, does it work for you?

 

<?php 
$s = 'There ___are__ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';
$s = preg_replace('/_([^_]+)_/m', '<i>\1</i>', $s);
$s = preg_replace('/\*([^*]+)\*/m', '<b>\1</b>', $s);
$s = preg_replace('/`([^`]+)`/m', '<code>\1</code>', $s);
echo htmlentities($s);
?>

 

Warmest wishes. :)

Link to comment
Share on other sites

$s = 'There ___are__ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';

 

I've received ouput:

There __<i>are</i>_ few things which you <code>need to ensure</code>` you would 1. educate (read all) 2. put into to use. **<b>Without those 2 points</b>* you can be sure, that your depressio

 

 

However, I've wanted to get:

$output = 'There <u>__are_</u> few things which you <code>need to ensure</code>` you would 1. educate (read all) 2. put into to use. <b>*Without those 2 points</b> you can be sure, that your depressio';

 

Could you handle that? I would also add, that double -> ** does bold, not singular one (*)

 

Thank you.

 

UPDATE. I want: // double ** **, __ __, `` `` <--- need to be (stick together)

that/to replace the code.

 

Thanks one more time.

Link to comment
Share on other sites

Hi Andy,

 

Try this.

 

<?php 
$s = 'There ___are__ few things which you `need to ensure`` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';
$s = preg_replace('/_(_*[^_]+_*)_/m', '<u>\1</u>', $s);
$s = preg_replace('/\*\*(\**[^*]+\**)\*\*/m', '<b>\1</b>', $s);
$s = preg_replace('/`([^`]+)`/m', '<code>\1</code>', $s);
echo htmlentities($s);
?>

 

 

Is that what you have in mind?

 

Link to comment
Share on other sites

Yes! Exactly.

 

The last thing is, how to modify _ -> __ & ` -> `` (that need to be double)

 

in:

 

 

$s = preg_replace('/_(_*[^_]+_*)_/m', '<u>\1</u>', $s);

$s = preg_replace('/`([^`]+)`/m', '<code>\1</code>', $s);

 

thank you,

problem almost solved

 

wish you happy New Year!

Link to comment
Share on other sites

Yes! Exactly.
UPDATE. I want: // double ** **, __ __, `` `` <--- need to be (stick together)

 

Hi Andy,

Just saw your update. So now you want doubles everywhere?

 

Try this.

 

<?php 
$s = 'There ___are__ few things which you ```need to ensure```` you would 1. educate (read all) 2. put into to use. ***Without those 2 points** you can be sure, that your depressio';
$s = preg_replace('/__(_*[^_]+_*)__/m', '<u>\1</u>', $s);
$s = preg_replace('/\*\*(\**[^*]+\**)\*\*/m', '<b>\1</b>', $s);
$s = preg_replace('/``(`*[^`]+`*)``/m', '<code>\1</code>', $s);
echo htmlentities($s);
?>

 

 

Pay attention to the test string, I have changed it so you can see how the regex behaves with extra quotes.

:)

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.