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

PHP MongoDB Store Object as String
curl post Json with addslashes
qTranslate-x - How to show just the flag on menu
Laravel registration: Insert two rows into separated tables
Force CSS updates in wordpress w/ wp_enqueue_script only works on localhost?
Getting only the child elements of the first element
RESTfull API for website and admin panel
How to Display Post Expiring plugin on posts data table wordpress plugin
dropdown selection message missing PHP
how to get all parameter from foreign key entity symfony 3
PHP concatenate &para to variable
Trying to pass ajax value from php to jquery in selectize
PHP Error: T_CONSTANT_ENCAPSED_STRING [duplicate]
.htaccess Remove trailing / unless directory
Don't allow new user, if a user has already logged in
Error Fetching http headers in wsdltophp.com

Categories

HOME
artificial-intelligence
knockout.js
firebase
keyboard
message-queue
dafny
magento-2.0
docker-cloud
google-play-services
mirc
ipv6
cdi
eddystone
roku
azure-graph-api
lda
nexus3
freertos
amazon-elb
trace32
closures
uima
boomi
node-webkit
dashdb
batch-rename
uislider
sybase-ase
ipa
multi-dimensional-scaling
tf-idf
math.js
spring-annotations
javax.persistence
fotorama
sca
gsp
xmlunit
counting
rails-postgresql
installanywhere
ms-dos
strapi
microsoft-ui-automation
stat
resampling
word-cloud
httphandler
permission-denied
sony-future-lab-n
sonatype
line-intersection
palindrome
ptvs
mifos
metabase
xmlbeans
maven-release-plugin
bbc-micro
deferred-rendering
apple-configurator
okta-api
ioexception
pack
boost-proto
textpattern
perceptron
nette
highest
calcite
extend
replicaset
web-api
kotlin-android-extensions
nanomsg
google-hangouts
cout
nidaqmx
stripe.net
forceclose
miglayout
pageviews
cpu-time
proxy-server
saga
lru
sly-scroller
eager-loading
html-frames
email-verification
xcode4.5
android-dialog
instance-variables
clearinterval
uipangesturerecognizer
type-safety
drawimage
qt-mobility
simile
bll
pyjamas
odac
principles
fgetc
cleartype
resharper-5.0
downcasting
xap
rakudo
watchpoint
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App