Solved

php substring remove between --

Posted on 2012-04-02
5
257 Views
Last Modified: 2012-06-27
I need to remove a string that is in between "--" (double dash) of a long string.

this-is-a-string--REMOVE--this-is-another-string

Open in new window

0
Comment
Question by:glynco
[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
5 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 37795270
try this:
$your_string = "this-is-a-string--REMOVE--this-is-another-string";
$pos = strpos($your_string,'--');
$left_part = substr($your_string,0,$pos-1);
$pos = strpos($your_string, '--', $pos+2);
$right_part = substr($your_string, $pos+2);
$your_string = $left_part . $right_part;

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37795283
Try like this
$str = "this-is-a-string--REMOVE--this-is-another-string";
echo preg_replace('/--+/','',$str);
OR

echo preg_replace('/[-]{2,}/','',$str);
0
 
LVL 15

Accepted Solution

by:
StingRaY earned 500 total points
ID: 37795293
<?php

$string = 'this-is-a-string--REMOVE--this-is-another-string--REMOVE ME TOO--the-rest-of-string';
$string = preg_replace('/(--)(.*)(--)/U','$1$3',$string);
echo $string;

?>

Open in new window


Result:

this-is-a-string----this-is-another-string----the-rest-of-string
0
 

Author Comment

by:glynco
ID: 37795344
StingRay when I change the string to something like this. The --coins-currency-- is not removed.

$string = '2012-04-02-165747--coins-currency--coins-title';
$string = substr(str_replace("-"," ", $string), 18);
$string = preg_replace('/(--)(.*)(--)/U','$1$3',$string);
echo $string;

Open in new window

0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37795370
Because of you have replaced "-" to " " already. Swap line 2 and 3 and see the result.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

737 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