Solved

How do I fix this preg-match-all For Each

Posted on 2015-01-17
4
75 Views
Last Modified: 2015-01-17
This preg_match_all statement works, however when the URL is imported into the database it looks like:
http://www.website.com/brand/airfonix/1  

I am assuming that the "1" is the $key...

How do I make the URL look like:

http://www.website.com/brand/airfonix/All      // (Without the "1")

instead of
 
http://www.website.com/brand/airfonix/1


$statement = $conn->prepare('INSERT IGNORE INTO Websiteurldownload (BrandURL) VALUES(:URL)');

preg_match_all('%<td width="13" height="30" align="left" valign="top"><a href="(.*?)">%',$buffer2,$matches1);


foreach($matches1[0] as $key=>$link){
	$params = array(':URL'=>"http://www.website.com/".$matches1[1][$key]);
        //$params = array(':URL'=>"http://www.website.com/".$matches1[1][$key]."/All");   
	$statement->execute($params); 
}

Open in new window

0
Comment
Question by:lawrence_dev
  • 2
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40555372
<?php // temp_lawrence_dev.php
error_reporting(E_ALL);

/** 
 * Replace the ending digits with the word "All"
 */
$url = 'http://www.website.com/brand/airfonix/1';
$rgx
= '#'       // REGEX DELIMITER
. '[0-9]'   // CHARACTER CLASS DIGITS
. '$'       // AT THE END OF THE DATA
. '#'       // REGEX DELIMITER
;
$new = preg_replace($rgx, 'All', $url);

var_dump($url, $new);

Open in new window

0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40555378
Try replacing line 3 with this:
preg_match_all('%<td width="13" height="30" align="left" valign="top"><a href="(.*)/.*?">%',$buffer2,$matches1);

This should give you the URL, minus the part after the last "/".

HTH,
Dan
0
 

Author Closing Comment

by:lawrence_dev
ID: 40555533
Simple t figure out.  Thanks for your help!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40555577
You're welcome.
Glad I could help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now