use preg_replace_callback with array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I know this has been asked elsewhere, but I can't find the precise situation (and understand it!) so I'm hoping someone might be able to help with code here.
There's an array of changes to be made. Simplified it's:
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('(w+)', function( $match )use( $change_to ) {
return $array[$match[1]];
}, $title);
I'm hoping to get back "Fred's girlfriend is called Tomasina" but I'm getting all sorts of stuff back depending on how I tweak the code - none of which works!
I'm pretty certain I'm missing something blindingly obvious so I apologise if I can't see it!
Thank you!
arrays preg-replace-callback
add a comment |
I know this has been asked elsewhere, but I can't find the precise situation (and understand it!) so I'm hoping someone might be able to help with code here.
There's an array of changes to be made. Simplified it's:
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('(w+)', function( $match )use( $change_to ) {
return $array[$match[1]];
}, $title);
I'm hoping to get back "Fred's girlfriend is called Tomasina" but I'm getting all sorts of stuff back depending on how I tweak the code - none of which works!
I'm pretty certain I'm missing something blindingly obvious so I apologise if I can't see it!
Thank you!
arrays preg-replace-callback
add a comment |
I know this has been asked elsewhere, but I can't find the precise situation (and understand it!) so I'm hoping someone might be able to help with code here.
There's an array of changes to be made. Simplified it's:
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('(w+)', function( $match )use( $change_to ) {
return $array[$match[1]];
}, $title);
I'm hoping to get back "Fred's girlfriend is called Tomasina" but I'm getting all sorts of stuff back depending on how I tweak the code - none of which works!
I'm pretty certain I'm missing something blindingly obvious so I apologise if I can't see it!
Thank you!
arrays preg-replace-callback
I know this has been asked elsewhere, but I can't find the precise situation (and understand it!) so I'm hoping someone might be able to help with code here.
There's an array of changes to be made. Simplified it's:
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('(w+)', function( $match )use( $change_to ) {
return $array[$match[1]];
}, $title);
I'm hoping to get back "Fred's girlfriend is called Tomasina" but I'm getting all sorts of stuff back depending on how I tweak the code - none of which works!
I'm pretty certain I'm missing something blindingly obvious so I apologise if I can't see it!
Thank you!
arrays preg-replace-callback
arrays preg-replace-callback
asked Nov 23 '18 at 15:29
arathraarathra
468
468
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There are several issues:
- Use
$change_to
in the anonymous function, not$array
- Use regex delimiters around the pattern (e.g.
/.../
,/w+/
) - If there is no such an item in
$change_to
return the match value, else, it will get removed (the check can be performed withisset($change_to[$match[0]])
).
Use
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('/w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina
See the PHP demo.
Also, if your string can contain any Unicode letters, use '/w+/u'
regex.
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add thei
flag to the pattern e.g./w+/i
it will not go case insensitive and in the above example an array item of"tom" => "Fred"
will not be found in the original string.
– arathra
Nov 23 '18 at 19:28
@arathraw
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of$change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.
– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449348%2fuse-preg-replace-callback-with-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are several issues:
- Use
$change_to
in the anonymous function, not$array
- Use regex delimiters around the pattern (e.g.
/.../
,/w+/
) - If there is no such an item in
$change_to
return the match value, else, it will get removed (the check can be performed withisset($change_to[$match[0]])
).
Use
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('/w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina
See the PHP demo.
Also, if your string can contain any Unicode letters, use '/w+/u'
regex.
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add thei
flag to the pattern e.g./w+/i
it will not go case insensitive and in the above example an array item of"tom" => "Fred"
will not be found in the original string.
– arathra
Nov 23 '18 at 19:28
@arathraw
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of$change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.
– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
add a comment |
There are several issues:
- Use
$change_to
in the anonymous function, not$array
- Use regex delimiters around the pattern (e.g.
/.../
,/w+/
) - If there is no such an item in
$change_to
return the match value, else, it will get removed (the check can be performed withisset($change_to[$match[0]])
).
Use
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('/w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina
See the PHP demo.
Also, if your string can contain any Unicode letters, use '/w+/u'
regex.
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add thei
flag to the pattern e.g./w+/i
it will not go case insensitive and in the above example an array item of"tom" => "Fred"
will not be found in the original string.
– arathra
Nov 23 '18 at 19:28
@arathraw
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of$change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.
– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
add a comment |
There are several issues:
- Use
$change_to
in the anonymous function, not$array
- Use regex delimiters around the pattern (e.g.
/.../
,/w+/
) - If there is no such an item in
$change_to
return the match value, else, it will get removed (the check can be performed withisset($change_to[$match[0]])
).
Use
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('/w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina
See the PHP demo.
Also, if your string can contain any Unicode letters, use '/w+/u'
regex.
There are several issues:
- Use
$change_to
in the anonymous function, not$array
- Use regex delimiters around the pattern (e.g.
/.../
,/w+/
) - If there is no such an item in
$change_to
return the match value, else, it will get removed (the check can be performed withisset($change_to[$match[0]])
).
Use
$title = "Tom's wife is called Tomasina";
$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);
$title = preg_replace_callback('/w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina
See the PHP demo.
Also, if your string can contain any Unicode letters, use '/w+/u'
regex.
answered Nov 23 '18 at 15:37
Wiktor StribiżewWiktor Stribiżew
329k16149228
329k16149228
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add thei
flag to the pattern e.g./w+/i
it will not go case insensitive and in the above example an array item of"tom" => "Fred"
will not be found in the original string.
– arathra
Nov 23 '18 at 19:28
@arathraw
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of$change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.
– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
add a comment |
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add thei
flag to the pattern e.g./w+/i
it will not go case insensitive and in the above example an array item of"tom" => "Fred"
will not be found in the original string.
– arathra
Nov 23 '18 at 19:28
@arathraw
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of$change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.
– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
LOL it's easy when you know how! Perfect - thank you!!!
– arathra
Nov 23 '18 at 15:47
Almost there now! But working through this I came across another problem - even if I add the
i
flag to the pattern e.g. /w+/i
it will not go case insensitive and in the above example an array item of "tom" => "Fred"
will not be found in the original string.– arathra
Nov 23 '18 at 19:28
Almost there now! But working through this I came across another problem - even if I add the
i
flag to the pattern e.g. /w+/i
it will not go case insensitive and in the above example an array item of "tom" => "Fred"
will not be found in the original string.– arathra
Nov 23 '18 at 19:28
@arathra
w
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of $change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.– Wiktor Stribiżew
Nov 23 '18 at 19:32
@arathra
w
does not need case insensitive flag, it match both upper- and lowercase chars. Since you are just matching any sequence of 1 or more word chars, you cannot control case sensitivity through regex, you need to do that yourself. I have just googled a possible workaround. A regex approach will depend on 1) the size of $change_to
, 2) If keys contain whitespaces, 3) if keys can contain special chars and can start/end with them.– Wiktor Stribiżew
Nov 23 '18 at 19:32
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
Once again I have to thank you, Wiktor! I spent hours trying to find the solution, moving in the direction of changing the array first, but couldn't find the right way till you linked to array_change_key_case. Cheers!
– arathra
Nov 24 '18 at 9:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449348%2fuse-preg-replace-callback-with-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown