Viewed   114 times

I wanted to make a PHP function that would make text bold between double asterisks, and italic between one asterisk, (quite like the editor on ).

Same rules apply, if there's a space between the * and the word, it shouldn't render.

Who can help me out? I tried to, but I only came this far, as I don't know how to make the odd asterisks "< b >" and the even ones "< /b >".

(I can't type them without the spaces, will render the text between as bold.....)

$thenewtext = str_replace("**", "<b>", "**Hello World** of PHP");

 Answers

4

A simple regex will do the trick:

$thenewtext = preg_replace('#*{2}(.*?)*{2}#', '<b>$1</b>', '**Hello World** of PHP');
Tuesday, August 9, 2022
5

I'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green).

$journalName = preg_replace('/s+/', '_', $journalName);

instead of:

$journalName = str_replace(' ', '_', $journalName);
Saturday, September 3, 2022
 
joekir
 
3

Update: I'd agree with others that the following is an easier-to-read alternative for most folks:

$page = str_replace("'", '"', $page);

My original answer:

$page = str_replace(chr(39), chr(34), $page);
Sunday, October 16, 2022
3

This should do what you want:

Sub BoldTags()
Dim X As Long, BoldOn As Boolean
BoldOn = False 'Default from start of cell is not to bold
For X = 1 To Len(ActiveCell.Text)
    If UCase(Mid(ActiveCell.Text, X, 3)) = "<B>" Then
        BoldOn = True
        ActiveCell.Characters(X, 3).Delete
    End If
    If UCase(Mid(ActiveCell.Text, X, 4)) = "</B>" Then
        BoldOn = False
        ActiveCell.Characters(X, 4).Delete
    End If
    ActiveCell.Characters(X, 1).Font.Bold = BoldOn
Next
End Sub

Currently set to run on the activecell, you can just plop it in a loop to do a whole column. You can easily adapt this code for other HTML tags for Cell formatting (ie italic etc)

This was in the cell I tested on (minus the space after <): Sample < b>Te< /b>st of < B>bolding< /B> end

The result was: Sample Test of bolding end

Hope that helps

Wednesday, September 28, 2022
 
alaa
 
1
<Text style={styles.bold}>I'm bold!</Text>
<Text style={styles.italic}>I'm italic!</Text>
<Text style={styles.underline}>I'm underlined!</Text>

const styles = StyleSheet.create({
    bold: {fontWeight: 'bold'},
    italic: {fontStyle: 'italic'},
    underline: {textDecorationLine: 'underline'}
})

Working demo on Snack: https://snack.expo.io/BJT2ss_y7

Sunday, September 11, 2022
 
bisko
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :