Home > Back-end >  How to find a given value in a string Laravel
How to find a given value in a string Laravel

Time:01-05

I get a string on request, using guzzle

 $import = new ServerInviteClient();
$response = $import->client->request('GET', 'cB2EDPJ9wr');
$items = $response->getBody()->getContents();
dd($items);

$id = 1234567

There are more than 1000 words in this line and I need to find the given value $id

I tried to search through whereLike, but it didn't work out, due to the fact that this is not a string in the database, but a query How do I find the values in this string?

-------Edit

Part of dd($item), I need to find $id = xqc in this line

<!DOCTYPE html><html ><head><meta charset="utf-8"><title>Twitch</title><meta property="og:title" content="xQc - Twitch"/><meta pr
operty="og:video:secure_url" content="https://player.twitch.tv/?channel=xqc&amp;player=facebook&amp;autoplay=true&amp;parent=meta.tag"/><meta property="
al:ios:url" content="twitch.tv/xqc"/><meta name="twitter:app:name:iphone" content="Twitch"/><meta name="twitter:app:id:iphone" content="id460177396"/><m
eta name="twitter:app:url:iphone" content="twitch.tv/xqc"/><meta property="og:type" content="video.other"/><meta property="og:video:height" content="378
"/><meta content="https://www.twitch.tv/xqc" property="og:url"/><meta name="title" content="xQc - Twitch"/><meta name="twitter:image" content="https://s
tatic-cdn.jtvnw.net/jtv_user_pictures/xqc-profile_image-9298dca608632101-300x300.jpeg"/><meta property="al:android:package" content="tv.twitch.android.a
pp"/><link rel="alternate" media="only screen and(max-width: 640px)" href="https://m.twitch.tv/xqc"/><meta name="twitter:description" content="{[][][][]
[][\][\]&#39; \&#39; &#39;\\&#39;][]\]\]\]\]\][]\[]\[\][[]\[]\;[]&#39;; .&#39;;. \;&#39;/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P[]\][\[][\]&#39;[\
&#39;][&#39;["/><meta property="og:image" content="https://static-cdn.jtvnw.net/jtv_user_pictures/xqc-profile_image-9298dca608632101-300x300.jpeg"/><met
a property="og:video" content="https://player.twitch.tv/?channel=xqc&amp;player=facebook&amp;autoplay=true&amp;parent=meta.tag"/><meta property="og:vide
o:type" content="text/html"/><meta property="al:ios:app_name" content="Twitch"/><meta name="twitter:app:id:googleplay" content="tv.twitch.android.app"/>
<meta name="twitter:app:id:ipad" content="id460177396"/><meta name="robots" content="index"/><meta name="twitter:title" content="xQc - Twitch"/><meta pr
operty="fb:app_id" content="161273083968709"/><meta property="al:android:app_name" content="Twitch"/><meta name="twitter:app:name:googleplay" content="T
witch"/><meta property="og:site_name" content="Twitch"/><meta property="og:description" content="{[][][][][][\][\]&#39; \&#39; &#39;\\&#39;][]\]\]\]\]\]
[]\[]\[\][[]\[]\;[]&#39;; .&#39;;. \;&#39;/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P[]\][\[][\]&#39;[\&#39;][&#39;["/><meta name="twitter:card" cont
ent="summary"/><meta name="twitter:site" content="@twitch"/><meta property="og:video:width" content="620"/><meta content="id460177396" property="al:ios:
app_store_id"/><meta name="twitter:app:name:ipad" content="Twitch"/><meta name="twitter:app:url:ipad" content="twitch.tv/xqc"/><meta name="description"
content="{[][][][][][\][\]&#39; \&#39; &#39;\\&#39;][]\]\]\]\]\][]\[]\[\][[]\[]\;[]&#39;; .&#39;;. \;&#39;/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P
[]\][\[][\]&#39;[\&#39;][&#39;["/><link rel="canonical" href="https://www.twitch.tv/xqc"/>...

CodePudding user response:

You could use the native php function strpos. This function returns the index of a substring in a string, or -1 if not found. You will need to validate that the returns value is not -1:

$id = 1234567

$import = new ServerInviteClient();
$response = $import->client->request('GET', 'cB2EDPJ9wr');
$items = $response->getBody()->getContents();

$found = strpos($items, $id) !== -1;

In php 8.0 , you could use the easier str_contains.

However, this is not the best approach. The id could be a found in another property of your items, and the results would be innacurate. A better approach is to decode the body of your response, and validate each items to see if it's id match.

You have not specified if the body is in JSON format, XML format or any other format, so we can't give you an exact answer, unfortunatly

  • Related