PHP preg expressions...

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

CreativeLogic

Established Member
Impact
3
I have this file full of text. It was actually a select drop down field. I want to insert everything into the database so I need to seperate everything in it even the optgroups. Ive tried everything I can think of...

Here is the file:Link

Here is an exampe of how I want it to be displayed (its the first one):
Code:
"Automobiles / Vehicles"
4X4/SUVs
Acura
Aston Martin
Audio / Video
BMW
Bugatti
Buick
Cadillac
Chevrolet
Chrysler
Citroen
Classic
Cobra
Convertibles
Electronics
Exotic
Ferrari
Ford
Formula 1
Hummer
Hydrogen Powered
Insurance
Jeeps
Land Rover
Land Rovers
Leased
Lexus
Mazda
Mercedes-Benz
Motorcycles
Muscle Cars
NASCAR
New
Nissan
Peugeot
Pickup Trucks
Pontiac
Porsche
Private Pilot
Racing
Recreational Vehicles (RVs)
Renault
Repair
Rolls Royce
Shopping
Sports Bikes
Taxi
Technology/Engineering
Toyota
Used
Volkswagen
Z cars
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
just inser them manually via phpmyadmin, will be alot quick and easier
 
0
Close
...Yea right... Would take at least twice as long...
 
0
Close
Hi, just use 2 "preg_match_all" function calls and a bit of php inbetween them to build up an array of optiongroups / options:

PHP:
<?php

$data = join("", file("http://www.yaproom.com/interests.txt"));

$optgroups = array();

if( preg_match_all('/<optgroup label="(.*?)">(.*?)<\/optgroup>/is', $data, $matches ) ){
	$cnt = count($matches[1]);
	for( $i = 0; $i < $cnt; $i++){
		$optgroups[$matches[1][$i]] = array();
		if( preg_match_all("/<option value='[0-9]*'>(.*?)<\/option>/is", $matches[2][$i], $options) ){
			$cnt2 = count($options[1]);
			for( $j = 0; $j < $cnt2; $j++){
				$option = $options[1][$j];
				$optgroups[$matches[1][$i]][] = $option;
			}
		}
	}
}

foreach( $optgroups as $n=>$options ){
	echo '"'.$n.'"';
	echo "\n";
	echo join("\n", $options)."\n";	
}

?>

That will print out a text list in the format you listed in your post (optgroup name in "" followed by a list of option names, etc)

$optgroups will be an array like this:

array (
"Option Group 1" => array("Option 1", "Option2", etc),
"Option Group 2" => array("Option 1", "Option2", etc),
etc, etc

So you can just step through the array and insert data into mysql with mysql_query()s...

hope that helps,

Sam
 
0
Close
Thanks I haven't tested it but that preg expressions looks like what I needed... Thanks...
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back