AndyPSV Posted December 29, 2011 Share Posted December 29, 2011 _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 Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 29, 2011 Author Share Posted December 29, 2011 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 Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 How can you do: "`" that does <code> Hi Andy, I don't think I have any idea of what you mean there. Would you please explain? Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 29, 2011 Author Share Posted December 29, 2011 $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 Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 29, 2011 Author Share Posted December 29, 2011 $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. Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 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? Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 29, 2011 Author Share Posted December 29, 2011 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! Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 29, 2011 Author Share Posted December 29, 2011 Yes, works as a charm. You've helped. Thanks! Quote Link to comment Share on other sites More sharing options...
ragax Posted December 29, 2011 Share Posted December 29, 2011 You're welcome. Have a good new year. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.