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

How to Locate Strings from Text Input in PHP

PHP中文网
PHP中文网forward
2024-11-13 09:42:16888browse

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

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"
}

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;
}
?>

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

{
  "TextContent": "string",
  "TargetRegex": "string",
  "MatchCase": true
}

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;
}
?>

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
}

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
}

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!

Statement:
This article is reproduced at:medium.com. If there is any infringement, please contact admin@php.cn delete