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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:10+00:00 2026-05-27T05:51:10+00:00

As the title reads, I’m trying to replace all non-alpha characters and replace all

  • 0

As the title reads, I’m trying to replace all non-alpha characters and replace all double (or more) white spaces to a single one. I simply can’t get around the white spaces stuff.

So far my preg_replace line:

$result = trim( preg_replace( '/\s+/', '', strip_tags( $data->parent_label ) ) );

Note: The strip_tags and trim is needed.


EDIT: This is what I came up with:

/**
 * Removes all non alpha chars from a menu item label
 * Replaces double and more spaces into a single whitespace
 * 
 * @since 0.1
 * @param (string) $item
 * @return (string) $item
 */
public function cleanup_item( $item )
{
    // Regex patterns for preg_replace()
    $search = [
        '@<script[^>]*?>.*?</script>@si', // Strip out javascript 
        '@<style[^>]*?>.*?</style>@siU',  // Strip style tags properly 
        '@<[\/\!]*?[^<>]*?>@si',          // Strip out HTML tags
        '@<![\s\S]*?–[ \t\n\r]*>@',       // Strip multi-line comments including CDATA
        '/\s{2,}/',
        '/(\s){2,}/',
    ];
    $pattern = [
        '#[^a-zA-Z ]#', // Non alpha characters
        '/\s+/',        // More than one whitespace
    ];
    $replace = [
        '',
        ' ',
    ];
    $item = preg_replace( $search, '', html_entity_decode( $item ) );
    $item = trim( preg_replace( $pattern, $replace, strip_tags( $item ) ) );

    return $item;
}

Maybe the last strip_tags() isn’t necessary. It’s just there to be sure.

  • 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-05-27T05:51:10+00:00Added an answer on May 27, 2026 at 5:51 am
    $patterns = array (
      '/\W+/', // match any non-alpha-numeric character sequence, except underscores
      '/\d+/', // match any number of decimal digits
      '/_+/',  // match any number of underscores
      '/\s+/'  // match any number of white spaces
    );
    
    $replaces = array (
      '', // remove
      '', // remove
      '', // remove
      ' ' // leave only 1 space
    );
    
    $result = trim(preg_replace($patterns, $replaces, strip_tags( $data->parent_label ) ) );
    

    …should do everything you want

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

Sidebar

Related Questions

The title should say it all, then I can solidify 2 more ticks on
I have a text title that reads This User The Title Of The Post
Hi all (before closing based on the title, please read the bottom parragraph), I'm
Title says what i'm trying to do. I can successfully generate an assembly if
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
First of all I'd welcome edits to the title of this question, I couldn't
I have an application that reads XML information about a vehicle title and parses
i want to read all links in .vm-video-title-divs and post them each in the
As the title reads... is this possibly? Workflow should be like: 1.- User is
as the title reads I'd like to find a webpage which presents an overview

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.