Home > Backend Development > PHP Tutorial > How to Locate Strings from Text Input in PHP

How to Locate Strings from Text Input in PHP

PHP中文网
Release: 2024-11-13 09:42:16
forward
895 people have browsed it

We can programmatically locate and retrieve content from a large text input string using two distinct approaches.

How to Locate Strings from Text Input in PHP

We can either 1) search inputs for text word-for-word (equivalent to a “CTRL   F” search in a document or browser), or we can 2) search for text via regex expression.

Using the code examples provided below, we can easily take advantage of two free APIs that carry out both search methods respectively. We can use either API with a free Cloudmersive API key (this will allow us to make up to 800 API calls per month with zero additional commitments).

First, let’s install the client SDK with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client
Copy after login

After that, let’s use the remaining code to call our respective functions.

To perform a simple text string match, we can structure our input request based on the following JSON example:

{
  "TextContent": "string",
  "TargetString": "string"
}
Copy after login

And we can call our function using the following code examples:

<?php require_once(__DIR__ . &#39;/vendor/autoload.php&#39;);

// Configure API key authorization: Apikey
$config = SwaggerClientConfiguration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new SwaggerClientApiEditTextApi(
    
    
    new GuzzleHttpClient(),
    $config
);
$request = new SwaggerClientModelFindStringSimpleRequest(); // SwaggerClientModelFindStringSimpleRequest | Input request

try {
    $result = $apiInstance->editTextFindSimple($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling EditTextApi->editTextFindSimple: ', $e->getMessage(), PHP_EOL;
}
?>
Copy after login

And to search via regex expression, we can structure our input request following the below JSON example:

{
  "TextContent": "string",
  "TargetRegex": "string",
  "MatchCase": true
}
Copy after login

And we can call our function using the below code:

<?php require_once(__DIR__ . &#39;/vendor/autoload.php&#39;);

// Configure API key authorization: Apikey
$config = SwaggerClientConfiguration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new SwaggerClientApiEditTextApi(
    
    
    new GuzzleHttpClient(),
    $config
);
$request = new SwaggerClientModelFindStringRegexRequest(); // SwaggerClientModelFindStringRegexRequest | Input request

try {
    $result = $apiInstance->editTextFindRegex($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling EditTextApi->editTextFindRegex: ', $e->getMessage(), PHP_EOL;
}
?>
Copy after login

We’ll get a unique response object for both requests. Our simple text string search will return text matches following the below example JSON structure:

{
  "Successful": true,
  "Matches": [
    {
      "CharacterOffsetStart": 0,
      "CharacterOffsetEnd": 0,
      "ContainingLine": "string"
    }
  ],
  "MatchCount": 0
}
Copy after login

And our regex expression search will structure our response like so:

{
  "Successful": true,
  "Matches": [
    {
      "CharacterOffsetStart": 0,
      "CharacterOffsetEnd": 0,
      "ContainingLine": "string",
      "MatchValue": "string",
      "MatchGroups": [
        "string"
      ]
    }
  ],
  "MatchCount": 0
}
Copy after login

That’s all there is to it!

Either solution can help us automate the process of retrieving specific content from text inputs in our PHP applications.

The above is the detailed content of How to Locate Strings from Text Input in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:medium.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template