?
Solved

php substring remove between --

Posted on 2012-04-02
5
Medium Priority
?
259 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 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