preg_match()
Moderator: General Moderators
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
preg_match()
hi
its me again.
I was wondering if there is any way to find something without something after it, such as
finding src="xxx" but not src="http://xxx"
some sample code would be very helpful! thanks
its me again.
I was wondering if there is any way to find something without something after it, such as
finding src="xxx" but not src="http://xxx"
some sample code would be very helpful! thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$p = '#src=\s*(["\'])?(?!http://)(.*?)\\1#';-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
ok, here's my code so far, and its not working:
$contents is the data
$address is the address (eg. http://www.google.com)
is it the (.*?) bits that are stopping it from working?
edit:
maybe i should have said my problem clearer:
i want it to find and replace src=" or href=" with src="$address or href="$address
its a double quote, and i also want the search thing to search for a match with or without the double quote[/i]
Code: Select all
$searchstring = array('#src=\s*(["])?(?!http://)(.*?)\\1#', '#href=\s*(["])?(?!http://)(.*?)\\1#');
$replacestring = array("src=\"$address", "href=\"$address");
$contents = str_replace($searchstring, $replacestring, $contents);$address is the address (eg. http://www.google.com)
is it the (.*?) bits that are stopping it from working?
edit:
maybe i should have said my problem clearer:
i want it to find and replace src=" or href=" with src="$address or href="$address
its a double quote, and i also want the search thing to search for a match with or without the double quote[/i]
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
it's not working how? The patter will match the entire attribute, not just the beginning part.
You can do what you are trying in 1 expression.
d11wtq | Ouch
You can do what you are trying in 1 expression.
Code: Select all
$searchstring = '#(src|href)=\s*(["\'])?(?!http://)(.*?)\\2#i';
$replacestring = "\\1=\\2{$address}\\3\\2";
$contents = str_replace($searchstring, $replacestring, $contents);-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
adding in http://.../
Code: Select all
<?php
if ($_POST['submit']) {
$address = $_POST['address'];
if (!$contents = file_get_contents($address)) {
echo "<b>Error: Cannot read file! Either file is empty or file does not exist.</b>";
exit;
}
$searchstring = '#(src|href)=\s*(["\'])?(?!http://)(.*?)\\2#i';
$replacestring = "\\1=\\2{$address}\\3\\2";
$contents = preg_replace($searchstring, $replacestring, $contents);
echo $contents;
}
?>src="xxx" to src="http://.../xxx" and href="xxx" to href="http://.../xxx"
problems:
*some srcs and hrefs don't have the quote or double quote... some have a slash or just a folder name.
*another problem is if the link is "../", ie. going up a directory
i think i can fix the folder name one, but can anyone help with the slash or the ../?
thanks in advance