php


Extract a value using regular expression


Say I have this string: fg_^"-kv:("value_i_want")frt. That string will stay constant except for value_i_want - which could change all the time. This little string is hidden within a huge string of data (constantly changing), I have saved in a PHP variable
Is there any I could do a Regular Expression on a string, let's say of 50000 characters to find that string (above) without knowing what the value_i_want is, and then saving the value in a variable - using PHP
You can just use strpos to find where the value you want is. You may need to tweak this a little, I haven't tested it. $long_string is the string you are searching for a match in.
$start = strpos($long_string, 'fg_^"-kv:("', 0);
$end = strpos($long_string, '")frt', $start);
$value_i_want = substr($long_string, $start+11, $end-$start-11);
Try this:
if (preg_match_all('/(?<=fg_\^\"\-kv:\(\")[a-z_]+(?=\"\)frt)/', $yourstring, $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
Pattern: (?<=fg_\^\"\-kv:\(\")[a-z_]+(?=\"\)frt)
(?<=fg_\^\"\-kv:\(\") - positive lookbehind assertion, means that searched string should be after exactly this string: fg_^"-kv:("
(?=\"\)frt) - positive lookahead assertion, means the same, but before:
")frt
[a-z_]+ - the string you are looking for, may consist of one or more letter and underscores. Change it for your needs.
Demo: https://regex101.com/r/vS7dI0/1

Related Links

How to limit a form submission according to database limit
DynamoDB Session Handler Site Wide Used with phpMyAdmin
Extract data from html table to csv by simple html dom
select first row next second, etc from query result
Why isn't this PHP Login form working?
How to post data to drupal commerce services?
i18n Smarty variable into lang()
rearrange countries wherever used
php pagination with date and not serial number
MySQL connection error in configuration file
PHP filter attachment more fast solution?
PHP replace multiple value using str_replace?
Trying to display my products in my netbeans php project
PHP function ord() returns wrong code of cirilyc charecter
How do i get variables from a html form using a session wrapper class in PHP?
PHPUnit was not found in your include_path

Categories

HOME
xpath
opencart
schema.org
haproxy
slider
lvm
header
video-streaming
tweepy
eddystone
mutation-testing
elastic-load-balancer
grocery-crud
texas-instruments
fosuserbundle
office-ui-fabric
nullreferenceexception
bazaar
adminlte
conda
batch-rename
amazon-sns
quantitative-finance
animated-gif
software-packaging
password-generator
marquee
math.js
javax.persistence
copy-protection
intune
progid
fotorama
ms-media-foundation
markov-chains
java-stream
testlink
stackpanel
django-crispy-forms
android-alarms
sbjson
utf
get-event-store
minikube
httphandler
rm
image-registration
icecast
brightscript
line-intersection
http-basic-authentication
glassfish-4.1
lvalue
alertify
jsf-2.3
frame-grab
django-redis
django-validation
calcite
apple
wso2ml
compositetype
modern.ie
azure-xplat-cli
android-audiomanager
findersync
uno
hp-idol-ondemand
vs-unit-testing-framework
global-scope
jquery-mobile-flipswitch
ecos
libgcc
stripe.net
webaii
jack
boolean-algebra
spring-security-acl
mft
facebook-game-groups
colon
hints
office-app
icenium
lynx
wndproc
live-wallpaper
sfinae
uitextfielddelegate
kobold2d
memory-pool
asio
getopts
servlet-container
accumulator
icon-language
isapi-redirect

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App