• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

replacing string from text

i have any text. i want to delete strings from this text.strings will start with abc and finish with xyz.i want to delete these strings.

i mean i want to delete

abc64643643634643634634xyz
abc64353343634634l_6jl43_k6jlk34j436xyz

i know the function preg_replace but i don't have any idea with patterns.

in php please
0
btocakci
Asked:
btocakci
  • 3
  • 2
1 Solution
 
ncooCommented:
Try:

<?php
$string = 'abc64353343634634l_6jl43_k6jlk34j436xyz';
$newstring = preg_replace('/^abc([a-zA-Z0-9_]*?)xyz$/i', '', $string);
echo $newstring;
?>
0
 
ncooCommented:
This will replace abc * xyz anywhere it appears in a string:

<?php
$string = 'yghjabc64353343634634l_6jl43_k6jlk34j436xyzytfghj';
$newstring = preg_replace('/abc([a-zA-Z0-9_]*?)xyz/i', '', $string);
echo $newstring."<br/>";//outputs yghjytfghj
?>

This will only replace abc * xyz where abc is at the start and xyz at the end
<?php
$string = 'abc64353343634634l_6jl43_k6jlk34j436xyz';
$newstring = preg_replace('/^abc([a-zA-Z0-9_]*?)xyz$/i', '', $string);
echo $newstring; //outputs nothing
?>
0
 
btocakciAuthor Commented:
sorry but it didn't work.i want to delete this string

<A onclick="MyWindow=window.open('pencere.php?link=178','Mywindow','toolbar=no,directories=no,top=100,left=100,status=no,scrollbars=yes,resizable=no,width=250,height=200');" href="#">

i mean i want this <A onclick="MyWindow".....whatever.....href="#">

and i use this code for this.

$bilgi = preg_replace('/<A onclick=\"MyWindow\"([a-zA-Z0-9_]*?)href=\"#\">$/i', '', $bilgi);

i echo $bilgi.but it is still same.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
btocakciAuthor Commented:
$x = preg_replace('/<A onclick=([a-zA-Z0-9_]*?)href="#">/i',"",$bilgi);

this didn't work too
0
 
SissonCommented:
preg_replace("/abc(.*?)xyz/", "$1", $string);
0
 
btocakciAuthor Commented:
thanks this worked

$bilgi = preg_replace('/<A onclick=(.*?)href="#">/',"$2",$bilgi);

but i dont know why i wrote $2 and how does it work :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now