Solved

RegEx search for repeating hyphens

Posted on 2011-02-20
4
665 Views
Last Modified: 2012-05-11
I am trying to replace multiple hyphens with a single one. Here is my php code to achive this

<?php
  $string = "a--b---c-d-e";
  $newstring = preg_replace("/(-)(\1+)/", "-", $string);
?>

Open in new window


I want the $newsting to be "a-b-c-d-e"

However, it does not do anything to the original string. Why is it not working?
0
Comment
Question by:hvs69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 125 total points
ID: 34936129
Try
<?php
  $string = "a--b---c-d-e";
  $newstring = preg_replace("/[-]+/", "-", $string);
  echo"$newstring";
?>
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34936273
i agree with @Roads. If you want something more general you can use

<?php
  $string = "a--b--  ## -c-d-..e(@)-g";
  $newstring = preg_replace("/[^a-z]+/", "-", $string);
  echo $newstring;
?>

this will eliminate everything between lowercase letters and puts a single hypen between..

This and @Roads solutions doesn't remove the trailing hypens: if you have

$string = '-a-b-';

it will keep as is. Do you need this too? You can do it in two easy steps (or in one with a more complex regexp)

<?php
  $string = "a--b---c-d-e";
  $newstring = preg_replace("/[^a-z]+/", " ", $string);
  $newstring = preg_replace("/ +/", "-", trim($string));
  echo"$newstring";
?>

0
 
LVL 1

Expert Comment

by:jfk013097
ID: 34936869
ereg_replace("--*", "-", $string)

will replace all multiple instances of - with a single one
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34937757
ereg functions are deprecated.

Regarding this, "it does not do anything to the original string." - that is correct, because the new value is assigned to the variable named $newstring.

http://www.laprbass.com/RAY_temp_hvs69.php

Outputs: a-b-c-d-e
<?php // RAY_temp_hvs69.php
error_reporting(E_ALL);

// TEST DATA FROM THE POST AT EE
$string = "a--b---c-d-e";

// CONSTRUCT A REGEX
$regex 
= '/'  // REGEX DELIMITER
. '-+' // MORE THAN ONE HYPHEN
. '/'  // REGEX DELIMITER
;

// REPLACE THE HYPHENS WITH ONE HYPHEN
$newstring = preg_replace($regex, '-', $string);
echo $newstring;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 68
blocking the posted data from outside of my domains 8 49
How to close my php post page. 3 41
Securing source code... theoretical question 10 48
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question