Joe
Joe

Reputation: 277

Searching for name in a string PHP

I have a string in php which I am trying to get the name from.

the string output is "| name=thomas | country=usa " etc

I want to be able to get the name "thomas" into a php variable.

Note: the data is dynamic so Im trying to find a way to search without hardcoding thomas.

Heres my code:

    $people= ($_GET['people']);

preg_match_all('/name="([^"]+)"/mi', $people, $matches);

var_dump ($matches[1]);

I receive an array to string conversion error in php.

Upvotes: 1

Views: 337

Answers (2)

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

function yourSearch($haystack) {
    $string= 'Enter value to search for';
    return(strpos($mystack, $string)); // or stripos() if you want case-insensitive searching.
}

$matches = array_filter($your_array, 'yourSearch');

Upvotes: 0

Use this regular expression

<?php
$str='| name=thomas | country=usa ';
preg_match_all('/=(.*?) /', $str, $matches);
print_r($matches[1][0]);  //"prints" thomas

enter image description here

Upvotes: 2

Related Questions