blazingkin
blazingkin

Reputation: 572

Non-specific string replacement in php

Essentially, I am trying to replace part of a string as long as it has the preceding part, but it might have something between the part that I need to check and the part I'm replacing

$file = str_replace('<a href=', '<a href=?pagename=',$file);

This code will catch most instances, but if I run into something like

<a class="irrelevant" href=......

it will not be able to catch it, is there a simple fix to this problem?

EDIT: More specifically, I am looking to only replace tagged links, not other elements on the page such as CSS links

Upvotes: 0

Views: 26

Answers (1)

Lewis
Lewis

Reputation: 14866

You don't need to replace <a. This is good enough:

$file = str_replace('href=', 'href=?pagename=',$file);

Upvotes: 1

Related Questions