Asked  7 Months ago    Answers:  5   Viewed   33 times

I have this data in a LONGTEXT column (so the line breaks are retained):

Paragraph one
Paragraph two
Paragraph three
Paragraph four

I'm trying to match paragraph 1 through 3. I'm using this code:

preg_match('/Para(.*)three/', $row['file'], $m);

This returns nothing. If I try to work just within the first line of the paragraph, by matching:

preg_match('/Para(.*)one/', $row['file'], $m);

Then the code works and I get the proper string returned. What am I doing wrong here?

 Answers

44

Use s modifier.

preg_match('/Para(.*)three/s', $row['file'], $m);

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Wednesday, March 31, 2021
 
Jimenemex
answered 7 Months ago
35

Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.

To find out if your variable has a newline at front or back, try this regex

var_dump(preg_match('/^n|n$/', $variable));

(I think you have to use single quotes so PHP doesn't turn your n into a literal newline in the string).

Wednesday, March 31, 2021
 
kinske
answered 7 Months ago
77

If you're just testing for the presence of the two words in the string you could use

'/couple.*2 pcs|2 pcs.*couple/' 
Saturday, May 29, 2021
 
Joegramming
answered 5 Months ago
60

Use the faster strpos if you only need to check for the existence of two numbers.

if(strpos($mystring, '|7|') !== FALSE AND strpos($mystring, '|11|') !== FALSE)
{
    // Found them
}

Or using slower regex to capture the number

preg_match('/|(7|11)|/', $mystring, $match);

Use regexpal to test regexes for free.

Saturday, May 29, 2021
 
smiggle
answered 5 Months ago
43

Your code is a over complicated. For a start the best solution for finding a location is to use the Haversine_formula.

I have simplified your code to use it.

<?php
if (isset( $_GET['lat'])){ 
    $lat = (float)$_GET['lat']; 
}  
if ( isset( $_GET['lon'])){ 
    $lon = (float)$_GET['lon']; 
}  
if ( isset( $_GET['radius'])){ 
    $radius = (float)$_GET['radius'];
} 
if ( isset( $_GET['q'])){ 
    $name = $_GET['q'];
} 

$dbh = new PDO('(censored private information');
//
$sql = "SELECT  name, lat, lon, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat) ) * cos( radians( lon ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lon) ) ) ) AS distance FROM location WHERE `name` LIKE '%s' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($lat),
mysql_real_escape_string($lon),
mysql_real_escape_string($lat),
mysql_real_escape_string($name),
mysql_real_escape_string($radius));

$q = $dbh->prepare( $sql );
$q->execute( $params );
$doc = new DOMDocument();
$r = $doc->createElement( "locations" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
  //$dlat = ( (float)$row['lat'] - $lat ) / LATMILES;
  //$dlon = ( (float)$row['lon'] - $lon ) / LONMILES;
  //]$d = sqrt( ( $dlat * $dlat ) + ( $dlon * $dlon ) );
  //if ( $d <= $radius ) {
    $e = $doc->createElement( "location" );
    $e->setAttribute( 'lat', $row['lat'] );
    $e->setAttribute( 'lon', $row['lon'] );
    $e->setAttribute( 'name', $row['name'] );
    $r->appendChild( $e );
  //}
}
echo $doc->saveXML();
?>

I have commented out some of your code as I don't think it is required.

I have also changed print $doc->saveXML(); toecho $doc->saveXML();

I would also try hard coding the parameters ie $lat =55.00; etc to ensure the required output is obtained.

The formula above i sin miles to change to km use 6371 instead of 3959

Saturday, May 29, 2021
 
PedroKTFC
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :