Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:15:31+00:00 2026-06-15T01:15:31+00:00

i have 2 text string in php and i want to change them to

  • 0

i have 2 text string in php and i want to change them to html tags

first :

*.some text
* some text

and second :

#.sometext
# sometext

i want to change the first string that start with '*.' or '* ' to <li> and end with '\r' or '\n' to </li>

and i want to change the second string that start with '#.' or '# ' to <ol> and end with '\r' or '\n' to </ol>

I wrote this regex code but not work:

$regex1 = "#([*.])(.*)([\n])#e";
$text = preg_replace($regex1,"('<li>$2</li>')",$text);

$regex2 = "#([#.])(.*)([\n])#e";
$text = preg_replace($regex2,"('<ol>$2</ol>')",$text);

what should i do?

thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:15:33+00:00Added an answer on June 15, 2026 at 1:15 am

    I recommend quite a few changes to your code, which I’ll explain below.

    First the code:

    <?php
    $text = "
    first :
    
    *.some text
    * some text
    
    and second :
    
    #.sometext
    # sometext
    ";
    
    $regex1 = "#\*(.| )(.*?)\n#";
    $text = preg_replace($regex1,"<li>$2</li>\n",$text);
    
    $regex2 = "#\#(.| )(.*?)\n#";
    $text = preg_replace($regex2,"<ol>$2</ol>\n",$text);
    
    echo $text;
    

    and the output

    first :
    
    <li>some text</li>
    <li>some text</li>
    
    and second :
    
    <ol>sometext</ol>
    <ol>sometext</ol>
    

    Changes:

    • You don’t need to use the e modifier in the regex
    • In order to match *. and *, just specify * (escaped to \*, and an OR condition for . (escaped to \.) and .
    • Use non-greedy version of .*, i.e. .*? so you only eat the least possible text.

    PS: This probably isn’t what you wanted (ol has this structure: <ol><li>...</li><li>...</li></ol>, but I wrote the code to your question’s spec. You should be able to modify this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PHP strings that have HTML inside of them, which I am storing
i have a string like this one: $string = some text http://dvz.local/index/index/regionId/28 http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
I have a text string that has been doctored to be web safe URLs
I have a text string with HTML embedded in it. It also has CRs
i have a text file with string abcdef I want to search for the
I have a string like: hello #this# is #some text string# text text I
I need to change the elements that surround some contents in a string, leaving
I have a jQuery variable. It's a text string. I want to create a
I have a block of text that has a particular image that I want
Possible Duplicate: Parse a JavaScript file through PHP I have some pretty long string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.