Jump to content

effigy

Staff Alumni
  • Posts

    3,600
  • Joined

  • Last visited

    Never

Posts posted by effigy

  1. Do you only want the first price? If so, you should use preg_match.

     

    <pre>
    <?php
    
    $data = <<<DATA
    FareAdult1.00 OFFERAdult1.00
    OfferAdult2.00 FareAdult15.00
    DATA;
    
    function regex_price($data) {
    	preg_match_all('/(?:Fare|OFFER)Adult([\d.]+)/', $data, $result_price, PREG_PATTERN_ORDER);
    	return $result_price;
    }
    
    print_r(regex_price($data));
    ?>
    </pre>
    

     

     

  2. $pieces = preg_split('/\S+/', $name);

    Effigy.. I'm rather curious.. As I examine your pattern, I can't help but wonder.. Since you are matching any non-space character one or more consecutive times.. is this pretty much a 'beefier' version of:

    '/\b\w+\b/' ? (since \w is basically a-zA-Z0-9_ (locale issues aside), I suppose your pattern anticipates a broader range of characters and let's the spaces act as 'word bounderies' or sorts)

     

    Essentially, yes. In many cases preg_split is better because you're being specific about what you do not want, rather than attempting to make a pattern that's versatile enough to match what you do want.

     

    The names are in all sorts of different formats

     

    What problems are you having with the given code? Do you need each variation to look for a middle name?

     

     

  3. Why are you using a regular expression if you want to insert all of the elements? Why can't you simply use $file from your example? Do you want to break the data apart by pairs of tags? What if these contain other tags?

  4. <pre>
    <?php
    $data = 'Zend_Db_Table_Abstract';
    echo preg_replace('/\b([A-Za-z]+(?:_[A-Za-z]+)+)\b/e', 'join("::", explode("_", "$1"))', $data);
    ?>
    </pre>
    

     

    Update:

     

    ...is there a way I can avoid this without knowing how many times $4 will match?

     

    It will use the last match saved into the backreference each time it needs to be used. If a new match is found by capturing parentheses, the previously saved match is overwritten.

  5. Without knowing all of the intricate ins and outs of SQL, that's probably the best approach. My take is below.

     

    You may also want to have a look at Perl's SQL::Statement. SQL::Statement::Structure pulls out the table names for you. Update: There's also SQL_Parser for PHP; I'm not sure what it has to offer.

     

    <pre>
    <?php
    $tests = array(
    	"A 'B C' D E F \"G\" H I 'J \"K\" L M' N O P \"Q 'R' S\" T U V 'W X Y' Z",
    	'A \'B C\' D E F "G" H I \'J "K" L M\' N O P "Q \'R\' S" T U V \'W\' X \'Y\' Z',
    );
    foreach ($tests as $test) {
    	$result = array();
    	### Split the string based on quote pairs.
    	$pieces = preg_split('/(([\'"]).*?\2)/', $test, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    	$num_pieces = count($pieces);
    	for ($i = 0; $i < $num_pieces; $i++) {
    		$piece = $pieces[$i];
    		### Skip lone quotes.
    		if ($piece == '"' || $piece == "'") {
    			continue;
    		}
    		### Only replace entries that are not quoted.
    		### Table name is "X" for sake of example.
    		$first = substr($piece, 0, 1);
    		$piece = $first == '"' || $first == "'" ?
    			$piece :
    			preg_replace('/\b(X)\b/', 'prefix_$1', $piece) ;
    		### Push.
    		array_push($result, $piece);
    	}
    	print_r($result);
    }
    ?>
    </pre>
    

  6. <pre>
    <?php
    $data = <<<DATA
    <img src="http://www.somewhere.com/image.jpg">
    Visit www.phpfreaks.com!
    <a href="http://www.google.com">http://www.google.com</a>
    See http://www.regular-expressions.info
    DATA;
    $pieces = preg_split('/(<[^>]+>)/', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    $num_pieces = count($pieces);
    for ($i = 0; $i < $num_pieces; $i++) {
    	### Skip tags or content of <a>.
    	if (substr($pieces[$i], 0, 1) == '<'
    	||  array_key_exists($i-1, $pieces)
    	&&  substr($pieces[$i-1], 0, 3) == '<a '
    	){
    		continue;
    	}
    	$pieces[$i] = preg_replace('%
    		(
    			(?:
    				(??:https?|ftp)://)
    				|
    				www\.
    			)
    			\S+
    			(?<!\p{P})
    		)
    	%x', '<a href="$1">$1</a>', $pieces[$i]);
    }
    echo htmlspecialchars(join('', $pieces));
    ?>
    </pre>
    

  7. I cannot find any information on this. My assumption is that it doesn't exist because there isn't a need for it. A data structure is being built when matching, so it's nice to be able to customize the key, whereas replacing is solely done by the engine.

     

    In the event that your replaces get nasty, the best approach is to use the /x modifier:

    /

    ### 1. Name

    (...)

    ### 2. Address

    (...)

    ### 3. And so forth...

    (...)

    /x

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.