Solved

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

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 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