在字符串中特定文本的末尾添加字符串

时间:2019-04-25 13:22:37

标签: php string

我需要在搜索词的末尾添加一个文本。

$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';

我需要在$insertStr中的'www.abc.com /'之后插入$baseStr。我只能使用strpos获得开始位置。

预期结果:

$resultStr = 'www.abc.com/xxx/cdf/?x=10';

编辑: 是否可以找到搜索字符串的结束位置并解决该问题?

3 个答案:

答案 0 :(得分:2)

您可以将<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.Cursor?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SeparatorMenuItem?> <?import javafx.scene.input.KeyCodeCombination?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane prefHeight="0.0" prefWidth="663.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.Menu"> <MenuBar fx:id="menuBar" layoutY="2.0" opacity="0.9" prefHeight="0.0" prefWidth="663.0" stylesheets="@../CSS/Menu.css"> <cursor> <Cursor fx:constant="DEFAULT" /> </cursor> <Menu mnemonicParsing="false" style="-fx-font-weight: bold;" styleClass="menu-title" text="STATISTIQUES"> <MenuItem accelerator="backspace" mnemonicParsing="false" onAction="#openHomepage" style="-fx-font-weight: normal;" text="Revenir à l'accueil " /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" onAction="#openSettings" text="Paramètres" /> <MenuItem mnemonicParsing="false" onAction="#showPatchnote" style="-fx-font-weight: normal;" text="Patchnote" /> <MenuItem mnemonicParsing="false" onAction="#changeLogs" style="-fx-font-weight: normal;" text="Changelogs" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem accelerator="Shortcut+Q" mnemonicParsing="false" onAction="#closeButtonAction" style="-fx-font-weight: normal;" text="Quitter Statistiques" /> </Menu> <Menu mnemonicParsing="false" text="Fichier"> <MenuItem mnemonicParsing="false" onAction="#print" text="Imprimer" /> </Menu> <Menu mnemonicParsing="false" text="Edit"> <MenuItem accelerator="Shortcut+Z" mnemonicParsing="false" text="Annuler" /> <MenuItem accelerator="Shortcut+Y" mnemonicParsing="false" text="Rétablir" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem disable="true" mnemonicParsing="false" text="Préférences" /> </Menu> <Menu mnemonicParsing="false" text="Département AVJ"> <MenuItem mnemonicParsing="false" onAction="#openSelectAvj" text="Menu de sélection" /> <MenuItem mnemonicParsing="false" onAction="#openContingentPage" text="Contingent" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" onAction="#openASDB" text="ASDB Engine" /> </Menu> <Menu mnemonicParsing="false" text="Département SI"> <MenuItem mnemonicParsing="false" onAction="#openSelectSi" text="Menu de sélection" /> <MenuItem mnemonicParsing="false" onAction="#openIndicateursPage" text="Indicateurs annuels" /> <MenuItem mnemonicParsing="false" onAction="#openComparaisonAnnees" text="Comparaison années" /> <MenuItem mnemonicParsing="false" onAction="#openComparaisonCentres" text="Comparaison centres" /> <MenuItem disable="true" mnemonicParsing="false" text="Comparaison régions" /> <Menu mnemonicParsing="false" onAction="#openSelectVisitesPatients" text="Comparaison Visites/Patients"> <MenuItem mnemonicParsing="false" onAction="#openVisitesCentres" text="Visites / Centres"/> <MenuItem mnemonicParsing="false" onAction="#openVisitesLocalites" text="Visites / Localités"/> <SeparatorMenuItem mnemonicParsing="false"/> <MenuItem disable="true" mnemonicParsing="false" text="Patients / Centres"/> <MenuItem disable="true" mnemonicParsing="false" text="Patients / Localités"/> </Menu> <SeparatorMenuItem mnemonicParsing="false" /> <Menu mnemonicParsing="false" text="Rapports"> <MenuItem mnemonicParsing="false" onAction="#pdfActivite" text="Rapport d'activité" /> <MenuItem mnemonicParsing="false" onAction="#pdfGestion" text="Rapport indicateurs de gestion" /> </Menu> </Menu> <Menu mnemonicParsing="false" text="Outils"> <MenuItem mnemonicParsing="false" onAction="#openASDB" text="ASDB Engine" /> <MenuItem mnemonicParsing="false" onAction="#detectJavaVersion" text="JAVA Version"> <accelerator> <KeyCodeCombination alt="UP" code="J" control="DOWN" meta="UP" shift="UP" shortcut="UP" /> </accelerator></MenuItem> <MenuItem mnemonicParsing="false" onAction="#openConnectionTest" text="Tester les connexions"> <accelerator> <KeyCodeCombination alt="UP" code="T" control="DOWN" meta="UP" shift="UP" shortcut="UP" /> </accelerator></MenuItem> </Menu> <Menu mnemonicParsing="false" text="?"> <MenuItem disable="true" mnemonicParsing="false" text="Aide" /> <MenuItem disable="true" mnemonicParsing="false" text="Signaler un bug" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" onAction="#openAboutWindow" text="À propos de Statistiques" /> </Menu> </MenuBar> </AnchorPane> 替换为import * as score from './score/reducers.js' import * as time from './time/reducers.js' $searchStr

$searchStr

给予

$insertStr

答案 1 :(得分:1)

在此处使用import VueRouter from 'vue-router' const routes = [ { path: '/', name: 'home', component: MainWindow, meta: { requiresAuth: true, } }, { path: '/login', name: 'login', component: Login, meta: { guest: true } } ] const Router = new VueRouter({ routes, mode: 'history', }); Router.beforeEach((to, from, next) => { const userData = localStorageAuth.getUserData(); if (to.matched.some(record => record.meta.requiresAuth)) { if (userData.token === null) { next({ path: '/login', params: {nextUrl: to.fullPath} }) } else { next(); } });

preg_replace

或者您可以使用$baseStr = 'www.abc.com/cdf/?x=10'; $searchStr = 'www.abc.com/'; $insertStr = 'xxx/'; $a = preg_replace('#'.$searchStr.'#', $searchStr.$insertStr, $baseStr); echo '<pre>'; print_r($a); //Output: www.abc.com/xxx/cdf/?x=10

str_replace

答案 2 :(得分:1)

您可以这样做,

echo str_replace($searchStr, $searchStr.$insertStr,$baseStr);

只需替换您的字符串,它将搜索并替换您。

Demo