Solved

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

Posted on 2015-01-17
4
76 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to count occurrences of each item in an array.

863 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

24 Experts available now in Live!

Get 1:1 Help Now