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 8602555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:06:51+00:00 2026-06-12T02:06:51+00:00

I am using Markdown in PHP and have written a regex to automatically wrap

  • 0

I am using Markdown in PHP and have written a regex to automatically wrap the text inside if any <h1>-<h6> tag with a named-anchor (<a name="Text">Text</a>) for linking.

Here is the PHP:

$text = '<p>This is a</p>
   <h1>Test</h1>
   <p>to see if this works </p>';

$regex = '/\<h([0-6]{1})\>(.+)\<\/h[0-6]{1}\>/';

echo preg_replace($regex, '<h$1><a name="$2">$2</a></h$1>', $text);

And the result is:

<p>This is a</p>
<h1><a name="Test">Test</a></h1>
<p>to see if this works </p>

The important thing here is, Markdown is light-weight and easy on storage space. Manually adding in the named-anchors kind of defeats the purpose of using Markdown altogether.

This method works fine for testing purposes, but I would like to know (and here is the question) if there is a better, more language-agnostic, way to accomplish this.

NOTE: The client this is for, wants it this way because they feel even the native Markdown syntax may alienate their more “computer-illiterate” users: # [Text](#Text). They simply want to type: # Text

  • 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-12T02:06:52+00:00Added an answer on June 12, 2026 at 2:06 am

    Seeing as you’re already using PHP, you should really take a look at the Markdown Extra implementation. From the MDE docs:

    With PHP Markdown Extra, you can set id attribute to headers. You should add the id prefixed by a hash inside curly brackets after the
    header at the end of the line, like this:

    Header 1            {#header1}
    ========
    
    ## Header 2 ##      {#header2}
    

    Then you can create links to different parts of the same document like this:

    [Link back to header 1](#header1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm using Markdown to format text input from user: http://michelf.com/projects/php-markdown/ But I'm doing
I'm using PHP Markdown (version 1.0.1n, updated October 2009) to display text saved to
I'm using the PHP markdown library: http://michelf.com/projects/php-markdown/ and the Javascript markdown library: http://attacklab.net/showdown/ I
Im using Markdown in Vim to write a lot of text. I'm wondering if
I'm using markdown a lot and would like to have a few pages that
I'm using http://michelf.com/projects/php-markdown/ for my markdown library and my question is that if i
Im using python markdown for my django project, when i have the value #/usr/bin/env
i m using wmd markdown editor in my project and i have a problem
Is there a PHP implementation of markdown suitable for using in public comments? Basically
Can I define a class name on paragraph using Markdown? If so, how?

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.