preg_replace and expression help needed...

I have to change this:

/105-6482156-1642019?%5Fencoding=UTF8&

to this:

script.php?

in $page ($page being the string it's found in).

the number between the   /   and the ?  changes each time the page loads.  So that would need to be set up so that any number and dashes between /   and  ?%5Fencoding=UTF8&   inclusive, would be replaced with:  script.php?  

Can you help with this?    Thank you,  Chris

St_Aug_Beach_BumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hube02Commented:
Do you want to do any thing with the digits and the dash? or just remove them?
0
Hube02Commented:
This will remove everything between the "/" and the "?" and replace it with "script.php"

Let me know if you want to do anything with the digits and dashes that are removed.

<?php
      
      $page = '/105-6482156-1642019?%5Fencoding=UTF8&';
      
      $page = preg_replace('#/([\d\-]*)(\?.*?)$#i', '/script.php\2', $page);
      echo $page;
      
?>
0
St_Aug_Beach_BumAuthor Commented:
Can't quite figure this out, but here is an example:

<?php
     
      $page = '<a href="/105-6482156-1642019?%5Fencoding=UTF8&node=569">Home Page</a><br/><a href="/105-6482156-1642019?%5Fencoding=UTF8&node=2">Apparel &amp; Accessories</a><br/>';
     
      $page = preg_replace('#/([\d\-]*)(\?.*?)$#i', '/script.php\2', $page);
      echo $page;
     
?>

and for a final result, after the preg replace, I need the url to be  /script.php?node=  (node number)

Your solution works somewhat on the first one, it leaves the  5Fencoding=UTF8&   part.  And then on the second one doesn't change it.  But I don't know why it wouldn't?  

No, I don't need the numbers/dashes.   Thanks for you help with this, in a bit over my head :)
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

St_Aug_Beach_BumAuthor Commented:
I should say:  I need the url(s) to be  /script.php?node=  (node number)
0
hieloCommented:
Try this:
$page = '<a href="/105-6482156-1642019?%5Fencoding=UTF8&node=569">Home Page</a><br/><a href="/105-6482156-1642019?%5Fencoding=UTF8&node=2">Apparel &amp; Accessories</a><br/>';    
 
$page = preg_replace('#["]/([^\?]+)([^"]+)#i','script.php?node=\1',$page);
 
echo $page;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hube02Commented:
This leaves only the node in the url string.
the () capture parts of your original string
to add any of the captures to the result use \# where # is the captured part
in the example below
\1 = 105-6482156-1642019
\2 = anything before "node=569"
\3 = "node=569"
\4 = anything that might be present after "node=569"

<?php
     
      $page = '<a href="/105-6482156-1642019?%5Fencoding=UTF8&node=569">Home Page</a><br/><a href="/105-6482156-1642019?%5Fencoding=UTF8&node=2">Apparel &amp; Accessories</a><br/>';
     
      $page = preg_replace('#/([\d\-]*)\?(.*?)(node=[\d]*)(.*?)$#i', '/script.php?\3', $page);
      echo $page;
     
?>
0
St_Aug_Beach_BumAuthor Commented:
Thank you all.

Hielo's solution worked well for me, though Hube02's answer helped me understand this a little better, so that I realized I had to change the number on Heilo's solution to '2' to get what I needed.  So I will split points with the bulk to Hielo.

Thanks again everyone!   Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.